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,4 @@
DROP TABLE IF EXISTS sessions;
DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS admins;

View File

@@ -0,0 +1,42 @@
CREATE TABLE admins (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR(190) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
role VARCHAR(32) NOT NULL DEFAULT 'admin',
active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (id), UNIQUE KEY uq_admins_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE sessions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
admin_id BIGINT UNSIGNED NOT NULL,
token_hash BINARY(32) NOT NULL,
expires_at TIMESTAMP(6) NOT NULL,
revoked_at TIMESTAMP(6) NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (id), UNIQUE KEY uq_sessions_token_hash (token_hash),
KEY idx_sessions_admin (admin_id), KEY idx_sessions_expiry (expires_at),
CONSTRAINT fk_sessions_admin FOREIGN KEY (admin_id) REFERENCES admins(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE customers (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(190) NOT NULL,
contact_email VARCHAR(190) NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'active',
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
updated_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (id), KEY idx_customers_status (status), KEY idx_customers_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE products (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
code VARCHAR(64) NOT NULL,
name VARCHAR(190) NOT NULL,
description TEXT NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
updated_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (id), UNIQUE KEY uq_products_code (code), KEY idx_products_active (active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

View File

@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS audit_events;
DROP TABLE IF EXISTS licenses;

View File

@@ -0,0 +1,32 @@
CREATE TABLE licenses (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
license_key CHAR(32) NOT NULL,
customer_id BIGINT UNSIGNED NOT NULL,
product_id BIGINT UNSIGNED NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'active',
starts_at TIMESTAMP(6) NOT NULL,
expires_at TIMESTAMP(6) NOT NULL,
idempotency_key VARCHAR(128) NOT NULL,
issued_by BIGINT UNSIGNED NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
revoked_at TIMESTAMP(6) NULL,
PRIMARY KEY (id),
UNIQUE KEY uq_licenses_key (license_key),
UNIQUE KEY uq_licenses_idempotency (issued_by, idempotency_key),
KEY idx_licenses_customer (customer_id), KEY idx_licenses_product (product_id), KEY idx_licenses_status (status),
CONSTRAINT fk_licenses_customer FOREIGN KEY (customer_id) REFERENCES customers(id),
CONSTRAINT fk_licenses_product FOREIGN KEY (product_id) REFERENCES products(id),
CONSTRAINT fk_licenses_admin FOREIGN KEY (issued_by) REFERENCES admins(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE audit_events (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
actor_admin_id BIGINT UNSIGNED NOT NULL,
action VARCHAR(64) NOT NULL,
resource_type VARCHAR(64) NOT NULL,
resource_id BIGINT UNSIGNED NOT NULL,
details JSON NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (id), KEY idx_audit_resource (resource_type, resource_id), KEY idx_audit_actor (actor_admin_id),
CONSTRAINT fk_audit_admin FOREIGN KEY (actor_admin_id) REFERENCES admins(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS license_operations;

View File

@@ -0,0 +1,14 @@
CREATE TABLE license_operations (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
license_id BIGINT UNSIGNED NOT NULL,
actor_admin_id BIGINT UNSIGNED NOT NULL,
operation VARCHAR(32) NOT NULL,
idempotency_key VARCHAR(128) NOT NULL,
request_fingerprint BINARY(32) NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (id),
UNIQUE KEY uq_license_operation_key (actor_admin_id, operation, idempotency_key),
KEY idx_license_operations_license (license_id),
CONSTRAINT fk_license_operations_license FOREIGN KEY (license_id) REFERENCES licenses(id),
CONSTRAINT fk_license_operations_admin FOREIGN KEY (actor_admin_id) REFERENCES admins(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

View File

@@ -0,0 +1,2 @@
DROP TABLE order_operations;
DROP TABLE offline_orders;

View File

@@ -0,0 +1,39 @@
CREATE TABLE offline_orders (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
order_no CHAR(32) NOT NULL,
customer_id BIGINT UNSIGNED NOT NULL,
product_id BIGINT UNSIGNED NOT NULL,
amount_cents BIGINT UNSIGNED NOT NULL,
currency CHAR(3) NOT NULL DEFAULT 'CNY',
status VARCHAR(32) NOT NULL DEFAULT 'pending_payment',
payment_reference VARCHAR(190) NULL,
paid_at TIMESTAMP(6) NULL,
paid_by BIGINT UNSIGNED NULL,
issued_license_id BIGINT UNSIGNED NULL,
created_by BIGINT UNSIGNED NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
updated_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (id), UNIQUE KEY uq_offline_orders_no (order_no),
UNIQUE KEY uq_offline_orders_license (issued_license_id),
KEY idx_offline_orders_customer (customer_id), KEY idx_offline_orders_status (status),
CONSTRAINT fk_offline_orders_customer FOREIGN KEY (customer_id) REFERENCES customers(id),
CONSTRAINT fk_offline_orders_product FOREIGN KEY (product_id) REFERENCES products(id),
CONSTRAINT fk_offline_orders_license FOREIGN KEY (issued_license_id) REFERENCES licenses(id),
CONSTRAINT fk_offline_orders_admin FOREIGN KEY (created_by) REFERENCES admins(id),
CONSTRAINT fk_offline_orders_paid_by FOREIGN KEY (paid_by) REFERENCES admins(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE order_operations (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
order_id BIGINT UNSIGNED NOT NULL,
actor_admin_id BIGINT UNSIGNED NOT NULL,
operation VARCHAR(32) NOT NULL,
idempotency_key VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
request_fingerprint BINARY(32) NOT NULL,
response JSON NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (id),
UNIQUE KEY uq_order_operations_key (actor_admin_id, operation, idempotency_key),
CONSTRAINT fk_order_operations_order FOREIGN KEY (order_id) REFERENCES offline_orders(id),
CONSTRAINT fk_order_operations_admin FOREIGN KEY (actor_admin_id) REFERENCES admins(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

View File

@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS activation_operations;
DROP TABLE IF EXISTS license_instances;

View File

@@ -0,0 +1,39 @@
CREATE TABLE license_instances (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
instance_id CHAR(36) NOT NULL,
license_id BIGINT UNSIGNED NOT NULL,
customer_id BIGINT UNSIGNED NOT NULL,
product_id BIGINT UNSIGNED NOT NULL,
secret_hash BINARY(32) NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'provisioned',
activated_at TIMESTAMP(6) NULL,
certificate_version VARCHAR(32) NULL,
certificate_payload JSON NULL,
certificate_signature TEXT NULL,
created_by BIGINT UNSIGNED NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
updated_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (id),
UNIQUE KEY uq_license_instances_instance (instance_id),
UNIQUE KEY uq_license_instances_license (license_id),
KEY idx_license_instances_customer (customer_id),
KEY idx_license_instances_product (product_id),
CONSTRAINT fk_license_instances_license FOREIGN KEY (license_id) REFERENCES licenses(id),
CONSTRAINT fk_license_instances_customer FOREIGN KEY (customer_id) REFERENCES customers(id),
CONSTRAINT fk_license_instances_product FOREIGN KEY (product_id) REFERENCES products(id),
CONSTRAINT fk_license_instances_admin FOREIGN KEY (created_by) REFERENCES admins(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE activation_operations (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
instance_id BIGINT UNSIGNED NOT NULL,
actor_admin_id BIGINT UNSIGNED NOT NULL,
idempotency_key VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
request_fingerprint BINARY(32) NOT NULL,
response JSON NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (id),
UNIQUE KEY uq_activation_operations_key (instance_id, idempotency_key),
CONSTRAINT fk_activation_operations_instance FOREIGN KEY (instance_id) REFERENCES license_instances(id),
CONSTRAINT fk_activation_operations_admin FOREIGN KEY (actor_admin_id) REFERENCES admins(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

View File

@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS instance_state_operations;
DROP TABLE IF EXISTS heartbeat_operations;
ALTER TABLE license_instances
DROP KEY idx_license_instances_last_seen,
DROP KEY idx_license_instances_lease,
DROP COLUMN freeze_state,
DROP COLUMN lease_expires_at,
DROP COLUMN last_seen_at,
DROP COLUMN client_seen_at,
DROP COLUMN observed_state;

View File

@@ -0,0 +1,35 @@
ALTER TABLE license_instances
ADD COLUMN last_seen_at TIMESTAMP(6) NULL,
ADD COLUMN client_seen_at TIMESTAMP(6) NULL,
ADD COLUMN observed_state VARCHAR(32) NOT NULL DEFAULT 'unknown',
ADD COLUMN lease_expires_at TIMESTAMP(6) NULL,
ADD COLUMN freeze_state VARCHAR(32) NOT NULL DEFAULT 'none',
ADD KEY idx_license_instances_last_seen (last_seen_at),
ADD KEY idx_license_instances_lease (lease_expires_at);
CREATE TABLE heartbeat_operations (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
instance_id BIGINT UNSIGNED NOT NULL,
idempotency_key VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
request_fingerprint BINARY(32) NOT NULL,
response JSON NOT NULL,
state VARCHAR(32) NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (id),
UNIQUE KEY uq_heartbeat_operations_key (instance_id, idempotency_key),
CONSTRAINT fk_heartbeat_operations_instance FOREIGN KEY (instance_id) REFERENCES license_instances(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE instance_state_operations (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
instance_id BIGINT UNSIGNED NOT NULL,
actor_admin_id BIGINT UNSIGNED NOT NULL,
idempotency_key VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
request_fingerprint BINARY(32) NOT NULL,
response JSON NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (id),
UNIQUE KEY uq_instance_state_operations_key (actor_admin_id,idempotency_key),
CONSTRAINT fk_instance_state_operations_instance FOREIGN KEY (instance_id) REFERENCES license_instances(id),
CONSTRAINT fk_instance_state_operations_admin FOREIGN KEY (actor_admin_id) REFERENCES admins(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS product_releases;

View File

@@ -0,0 +1,15 @@
CREATE TABLE product_releases (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
product_id BIGINT UNSIGNED NOT NULL,
version VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
storage_key CHAR(68) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
size_bytes BIGINT UNSIGNED NOT NULL,
published BOOLEAN NOT NULL DEFAULT TRUE,
created_by BIGINT UNSIGNED NOT NULL,
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY(id),
UNIQUE KEY uq_releases_version(product_id,version),
CONSTRAINT fk_release_product FOREIGN KEY(product_id) REFERENCES products(id),
CONSTRAINT fk_release_admin FOREIGN KEY(created_by) REFERENCES admins(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS package_objects;

View File

@@ -0,0 +1,5 @@
CREATE TABLE package_objects (
sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY,
size_bytes BIGINT UNSIGNED NOT NULL
) ENGINE=InnoDB;
INSERT INTO package_objects(sha256,size_bytes) SELECT sha256,MAX(size_bytes) FROM product_releases GROUP BY sha256;

View File

@@ -0,0 +1,2 @@
DROP INDEX idx_audit_created_id ON audit_events;
DROP TABLE IF EXISTS system_settings;

View File

@@ -0,0 +1,11 @@
CREATE TABLE system_settings (
id TINYINT UNSIGNED NOT NULL,
site_name VARCHAR(100) NOT NULL DEFAULT '授权管理平台',
support_email VARCHAR(254) NOT NULL DEFAULT '',
announcement TEXT NOT NULL,
updated_at TIMESTAMP(6) NULL,
PRIMARY KEY (id),
CONSTRAINT chk_system_settings_singleton CHECK (id = 1)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO system_settings(id,announcement) VALUES(1,'');
CREATE INDEX idx_audit_created_id ON audit_events(created_at,id);

View File

@@ -0,0 +1,2 @@
ALTER TABLE admins DROP INDEX uq_admins_username;
ALTER TABLE admins DROP COLUMN username;

View File

@@ -0,0 +1,4 @@
ALTER TABLE admins ADD COLUMN username VARCHAR(32) NULL AFTER id;
UPDATE admins SET username=CONCAT('admin-', id) WHERE username IS NULL;
ALTER TABLE admins MODIFY COLUMN username VARCHAR(32) NOT NULL;
ALTER TABLE admins ADD UNIQUE KEY uq_admins_username (username);

View File

@@ -0,0 +1,29 @@
# MySQL migration baseline
Status: DEV schemas are implemented through version 8 (local releases and digest-object locking). Formal entitlement policy acceptance and seed data remain pending. Read-only verification on 2026-09-30: business license_platform is version=7 dirty=false; isolated license_download_260930 is version=8 dirty=false. No business database migration was executed during the deployment-delivery slice.
## Version 007 to 008
007 stores release metadata; 008 adds package_objects digest rows used for SELECT FOR UPDATE serialization and backfills them from existing releases using MAX(size_bytes). Before applying 008, quiesce uploads, back up database and private files, and verify every digest has a single consistent size and an existing matching object; MAX is not an integrity check or repair. Explicit up must finish clean before using the new publication code. Historical migrations are unchanged. 008 down drops the lock inventory and must not be used while new upload handlers are running; 007 down destroys release metadata. Do not run either against business data without backup and approval.
A file is linked into its digest path before the metadata/audit transaction commits. Ordinary publication failures remove only files linked by that request, preserving pre-existing shared digest objects. Commit acknowledgement failure retains content because commit may have succeeded. Crashes can leave temporary fragments or unreferenced digest files; SQL and filesystem are not one atomic transaction. Orphan reconciliation/automatic GC is NOT implemented.
Future reconciliation must quiesce publishers or participate in the same digest locks, inventory product_releases/package_objects and private files, enforce a safety age window, recheck references immediately before quarantine, and verify checksums. Never remove a file based only on a missing row observed during an active upload or an uncertain commit. Keep quarantine recoverable and require reviewed deletion. U07 chunk/resume and crash cleanup remain open.
Version 6 adds independent lease/last-seen fields and heartbeat/freeze operation records. It was explicitly applied to the dedicated MySQL integration database on 2026-09-30. Its down migration drops operation history and lease state; do not execute against existing business data without backup and approval. The v6 isolated down/up rehearsal is still pending. Startup never applies migrations.
Use versioned pairs named `000001_description.up.sql` and `000001_description.down.sql`. Never edit an applied migration; add a new version. Seed permissions with stable unique codes, never commit an administrator password. SQL targets MySQL 8.0 with UTC timestamps and utf8mb4.
MySQL DDL may commit implicitly. Do not assume a failed schema migration is transactionally reversible. Before release, run the chosen migration runner against an empty MySQL 8 database, test each down/up pair with fixtures, and record dirty-version recovery. Down migrations that remove business data require backup and explicit operator confirmation.
The runner is `go run ./cmd/migrate` from `backend`. It accepts a golang-migrate MySQL URL such as `mysql://user:password@tcp(host:3306)/database?multiStatements=true` or `MIGRATION_DSN`. Supported actions are `up`, `down`, `version`, and explicit `force -version N` (flags must precede the action); `force` is only for an operator-reviewed dirty migration and never runs automatically.
```text
go run ./cmd/migrate -dsn "$MIGRATION_DSN" up
go run ./cmd/migrate -dsn "$MIGRATION_DSN" version
go run ./cmd/migrate -dsn "$MIGRATION_DSN" -version 12 force
``` The server does not run migrations on startup.
On 2026-09-30, a dedicated MySQL 8.0 container was used to verify an empty database up/down cycle and an intentionally invalid migration: failure left version 2 dirty, normal `up` was blocked, and explicit force to the last known-good version restored clean metadata. The probe migrations and container were removed after the test. Version 3 adds transactional license operation idempotency records for revoke/renew; it must be applied explicitly after version 2. No placeholder SQL is considered an executed business migration; formal entitlement schemas and CI database integration remain T04/T03 acceptance work.
Schema 10 adds unique administrator usernames. Existing accounts become `admin-{id}` and retain their passwords; login requests must send `username` instead of `email`. Run migrations before starting the current server or `admin-init`.