feat: implement license platform business flows and delivery tooling
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation.

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-09 15:33:22 +08:00
parent 3ab651d3a8
commit 54c318c615
142 changed files with 11105 additions and 178 deletions

View File

@@ -0,0 +1,53 @@
# Deployment delivery rehearsal
Status: development scaffolding, not production accepted. Existing API 18081 is not restarted by this work. Do not reuse its in-memory signing key lifecycle for deployment.
## Files and configuration
The root compose.yaml builds deploy/Dockerfile.api and deploy/Dockerfile.web. Only the web port is bound to host loopback (18083). MySQL, Redis, API and package storage have no published host ports. The private service network is internal. Redis is optional under the `cache` profile; the API currently does not use Redis for sessions or rate limiting.
Set these non-secret environment paths externally (not secret values in .env): MYSQL_APP_SECRET_FILE, MYSQL_ROOT_SECRET_FILE, LICENSE_SIGNING_KEY_HOST_FILE, PACKAGE_STORAGE_HOST. All refer to protected, pre-existing absolute host paths outside Git and the build context. App DB password must be a randomly generated URL-safe value using letters, digits, underscores and hyphens. The image reads it at runtime, never logs it; driver DSNs remain process environment secrets. Compose local secrets are bind mounts, not an encrypted secret manager. Secure their host ACLs, process access, backups and crash dumps.
The package directory must be writable by API UID 10001 and readable by Nginx, on a filesystem supporting hard links. Nginx mounts it read-only, serves it only through an internal location, disables directory listing and symlinks. Do not put it in the frontend/public tree. The API is non-root with read-only rootfs, all capabilities dropped and no-new-privileges; this is not a complete container-hardening assessment.
Run from the repository root, using an independent Compose project/database and protected persistent key:
```sh
docker compose config --quiet
docker compose build
docker compose up -d mysql
# Optional only: docker compose --profile cache up -d redis
docker compose --profile tools run --rm migrate
docker compose up -d api web
```
Migrations are explicit; startup never migrates. The migration profile uses the development app account, which also has DDL privileges in this local profile. Production needs separate least-privilege runtime and migration accounts. No administrator account/password is created by Compose. Stop/restart only this rehearsal project, never the existing 18081 process. Do not run down -v against retained data.
## Operations slice
See [operations-security.md](operations-security.md) for JSON logging, optional loopback metrics, local per-process entry limits, cookie/origin/body controls and CI evidence. New server main exposes only liveness publicly; readiness and metrics use numeric-loopback METRICS_ADDR (Compose 19090). Redis Lua shared entry limits are now default, with runtime failure returning 503 and no automatic fallback; local-dev must be explicitly selected for controlled development. ENTRY_REDIS_ADDR must be a numeric private/loopback IP; ENTRY_REDIS_PASSWORD_FILE optionally injects a protected absolute password file. Production Redis TLS/ACL/HA/capacity remain gates; the current Compose cache profile is not automatically wired to the API. Two isolated new API subprocess starts passed key-continuity and internal HTTP checks; unchanged 18081 still runs old source. See the release-gate update in operations-security.md for evidence limits.
## TLS, proxy trust and Redis
The loopback HTTP listener is for local rehearsal, not internet exposure. `deploy/nginx/tls-proxy.conf.example` is an example requiring managed CA certificates mounted read-only; it is deliberately not enabled by default and contains no development/self-signed key. Production must restrict upstream reachability, set `SESSION_COOKIE_SECURE=true`, and set `HTTP_TRUSTED_PROXY_CIDRS` to exact proxy source CIDRs only. Config rejects malformed CIDRs and `0.0.0.0/0`/`::/0`; Gin trusts no proxies by default. Nginx overwrites forwarded headers rather than forwarding client-supplied values. Direct TLS also sets Secure. Automated unit tests cover forced Secure and reject treating a bare X-Forwarded-Proto header as trusted. TLS deployment wiring, real certificate verification, ingress restrictions, CSRF/origin controls and end-to-end proxy spoof tests remain outstanding.
Redis is optional and not used by the current API for sessions, rate limits, or authoritative state. It is under Compose profile `cache`; API startup/readiness does not depend on it. Do not claim Redis-backed behavior.
The API has `/api/v1/health/live` for process liveness and `/api/v1/health/ready` for database connectivity plus exactly one clean migration row at schema version 8. Startup refuses a database that cannot be pinged or is not exactly at clean version 8; startup never migrates. Readiness returns only generic 503 on DB/schema failure, without internal error details. Compose API/web health checks use readiness. Unit tests cover unavailable DB, no migration row, old/future version and dirty schema; full Compose fault injection is pending. Load/capacity testing, pre-auth rate limiting, pinned image digests and vulnerability remediation remain open.
## Backup and restore
`scripts/backup-delivery.sh` creates a MySQL logical dump and deterministic tar of private package storage with checksums. Run only during a maintenance window that quiesces uploads and administrative writes: MySQL `--single-transaction` does not make a filesystem snapshot atomic with SQL. Protect/encrypt the destination and record schema version/checkpoint separately. Signing keys are intentionally excluded; recover them separately from protected secret management, never from SQL or these scripts. Redis is optional and holds no authoritative platform data.
`scripts/restore-delivery.sh` requires `CONFIRM_EMPTY_RESTORE=YES` and independently refuses both nonempty package directories and databases containing tables. YES does not bypass either check. Supply only a trusted backup and an isolated empty DB/private directory with no listener; the script does not validate arbitrary or malicious SQL/tar inputs. After restore, run explicit migration/version checks and compare all `product_releases`/`package_objects` references, file size/SHA-256 and key fingerprint before any traffic. The restore command is not a substitute for application-level reconciliation. Scripts use MySQL CLI and protected `MYSQL_PASSWORD_FILE`; ensure the image/host has mysql client tools. A minimal isolated dump/restore drill has passed; full-platform reconciliation, production recovery objectives, encrypted backup handling and a complete application recovery drill remain pending.
## Executed evidence (2026-09-30)
- docker compose config --quiet: passed with placeholder external paths; no real secrets were printed.
- Web image build: passed, including npm ci and typecheck/Vite build. npm reported two moderate dependency vulnerabilities; no forced upgrade performed. Existing 1.13 MB bundle warning remains.
- API image build: NOT passed. Earlier attempts failed on a base-image layer and DNS. The latest retry resolved proxy.golang.org but TCP/443 was refused during go mod download. No fallback registry or weakened checksum verification was configured.
- Full Compose start, migration and admin/login smoke: NOT executed. API image remains blocked by outbound Docker network refusal to proxy.golang.org; no local Go module cache image was available. Protected deployment inputs are not provisioned.
- Readiness unit tests passed for clean v8, old/future/dirty/empty/unavailable schema and nil DB; startup schema gate now requires exactly version 8 clean. Compose config with protected placeholder paths passed and uses readiness healthchecks. Redis is now an explicit optional `cache` profile because the current API does not use it.
- Isolated backup/restore rehearsal passed in a disposable MySQL 8.0 container: logical dump plus package tar checksums verified, restored schema row was 8 clean, package object size/digest and restored file SHA-256 matched. The signing key was excluded. The final restore used a new empty `license_restore_empty` database and an empty private directory; a second attempt against the nonempty database was rejected. This used a minimal synthetic dataset, not the full business database. Rehearsal containers were stopped; business MySQL and 18081 were untouched.
- Independently executed isolated MySQL/Nginx download integration: passed actual bytes/digest, Range 206, internal-path 404 and unauthenticated denial, plus expired/unpublished/missing release denial audits. This is not equivalent to full Compose acceptance.
- Go test/vet, npm test (10 cases), npm build: passed. Manual logged-in browser acceptance remains separate and pending.