为什么:同步入口此前只会拒绝;授权、预备份、四类写入、验证与失败恢复都没有代码。
本轮把编排与授权实现出来,真实生产执行仍按授权闸口关闭。
- internal/libraryrelease:预备份 → 数据库/资源/索引/Wiki → 验证 → 失败恢复;
授权过期/目标不符/快照不符/参数变化在动生产前拒绝;恢复失败单独上报。
- 迁移 000067 library_release_authorizations(目标+快照+参数摘要+有效期,可撤销)。
- POST/GET /api/v1/library/authorizations 与 /{id}/revoke,仅管理员签发,5–1440 分钟。
- worker library.sync 接编排:先闸口再授权,阶段落库,只有编排成功才标成功。
- 生产目标适配器未配置时显式失败(不返回零值状态),预备份存储同。
- overview 返回授权状态,页面"生产保护与恢复"显示签发人与有效期。
- 验收矩阵区分"已实现·隔离测试通过"与"真实执行未授权"。
This commit is contained in:
@@ -300,3 +300,72 @@ FROM library_versions WHERE scope=$1 ORDER BY created_at DESC, id DESC LIMIT $2`
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
// CreateLibraryAuthorization 记录一次生产发布授权;同一 (scope,snapshot,digest) 只保留最新有效。
|
||||
func (s *Store) CreateLibraryAuthorization(ctx context.Context, item governance.LibraryReleaseAuthorization) error {
|
||||
if item.ID == "" || item.Scope == "" || item.SnapshotID == "" || item.ParamsDigest == "" || item.ExpiresAt.IsZero() {
|
||||
return errors.New("invalid library release authorization")
|
||||
}
|
||||
_, err := s.db.Exec(ctx, `
|
||||
INSERT INTO library_release_authorizations (id, scope, snapshot_id, params_digest, authorized_by, expires_at, created_at)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7)`,
|
||||
item.ID, item.Scope, item.SnapshotID, item.ParamsDigest, item.AuthorizedBy, item.ExpiresAt.UTC(), item.CreatedAt.UTC())
|
||||
if err != nil {
|
||||
return fmt.Errorf("create library release authorization: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ActiveLibraryAuthorization 只返回"未撤销、未过期、且参数摘要完全一致"的授权。
|
||||
// 参数一变就查不到,自然拒绝,而不是靠调用方记得复核。
|
||||
func (s *Store) ActiveLibraryAuthorization(ctx context.Context, scope, snapshotID, paramsDigest string, now time.Time) (governance.LibraryReleaseAuthorization, bool, error) {
|
||||
var item governance.LibraryReleaseAuthorization
|
||||
err := s.db.QueryRow(ctx, `
|
||||
SELECT id, scope, snapshot_id, params_digest, authorized_by, expires_at, revoked_at, created_at
|
||||
FROM library_release_authorizations
|
||||
WHERE scope=$1 AND snapshot_id=$2 AND params_digest=$3 AND revoked_at IS NULL AND expires_at > $4
|
||||
ORDER BY expires_at DESC, created_at DESC
|
||||
LIMIT 1`, scope, snapshotID, paramsDigest, now.UTC()).Scan(&item.ID, &item.Scope, &item.SnapshotID,
|
||||
&item.ParamsDigest, &item.AuthorizedBy, &item.ExpiresAt, &item.RevokedAt, &item.CreatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return governance.LibraryReleaseAuthorization{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return governance.LibraryReleaseAuthorization{}, false, fmt.Errorf("load library authorization: %w", err)
|
||||
}
|
||||
return item, true, nil
|
||||
}
|
||||
|
||||
func (s *Store) RevokeLibraryAuthorization(ctx context.Context, id string, now time.Time) error {
|
||||
tag, err := s.db.Exec(ctx, `UPDATE library_release_authorizations SET revoked_at=$2 WHERE id=$1 AND revoked_at IS NULL`, id, now.UTC())
|
||||
if err != nil {
|
||||
return fmt.Errorf("revoke library authorization: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() != 1 {
|
||||
return errors.New("library authorization not found or already revoked")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) LibraryAuthorizations(ctx context.Context, scope string, limit int) ([]governance.LibraryReleaseAuthorization, error) {
|
||||
if limit < 1 || limit > 100 {
|
||||
return nil, errors.New("library authorization limit out of range")
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT id, scope, snapshot_id, params_digest, authorized_by, expires_at, revoked_at, created_at
|
||||
FROM library_release_authorizations WHERE scope=$1 ORDER BY created_at DESC, id DESC LIMIT $2`, scope, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list library authorizations: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
result := make([]governance.LibraryReleaseAuthorization, 0, limit)
|
||||
for rows.Next() {
|
||||
var item governance.LibraryReleaseAuthorization
|
||||
if err := rows.Scan(&item.ID, &item.Scope, &item.SnapshotID, &item.ParamsDigest, &item.AuthorizedBy,
|
||||
&item.ExpiresAt, &item.RevokedAt, &item.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scan library authorization: %w", err)
|
||||
}
|
||||
result = append(result, item)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS library_release_authorizations;
|
||||
@@ -0,0 +1,14 @@
|
||||
-- 生产发布授权:绑定确切目标 + 快照 + 参数摘要 + 有效期,可撤销。
|
||||
-- 参数一变(换快照/换目标)摘要就不同,旧授权不能再放行。
|
||||
CREATE TABLE library_release_authorizations (
|
||||
id VARCHAR(64) PRIMARY KEY,
|
||||
scope VARCHAR(16) NOT NULL CHECK (scope IN ('internal','external')),
|
||||
snapshot_id VARCHAR(64) NOT NULL,
|
||||
params_digest VARCHAR(64) NOT NULL,
|
||||
authorized_by VARCHAR(128) NOT NULL,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
revoked_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
CREATE INDEX library_release_authorizations_lookup
|
||||
ON library_release_authorizations(scope, snapshot_id, params_digest, expires_at DESC);
|
||||
@@ -11,13 +11,18 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("loadMigrations() error = %v", err)
|
||||
}
|
||||
if len(migrations) != 66 {
|
||||
t.Fatalf("migration count = %d, want 66", len(migrations))
|
||||
if len(migrations) != 67 {
|
||||
t.Fatalf("migration count = %d, want 67", len(migrations))
|
||||
}
|
||||
if last := migrations[len(migrations)-1]; last.Version != 66 ||
|
||||
!strings.Contains(last.UpSQL, "CREATE TABLE library_versions") ||
|
||||
!strings.Contains(last.UpSQL, "library_jobs_active") {
|
||||
t.Fatalf("migration 66 = %#v", last)
|
||||
if last := migrations[len(migrations)-1]; last.Version != 67 ||
|
||||
!strings.Contains(last.UpSQL, "library_release_authorizations") ||
|
||||
!strings.Contains(last.UpSQL, "params_digest") {
|
||||
t.Fatalf("migration 67 = %#v", last)
|
||||
}
|
||||
if prev := migrations[len(migrations)-2]; prev.Version != 66 ||
|
||||
!strings.Contains(prev.UpSQL, "CREATE TABLE library_versions") ||
|
||||
!strings.Contains(prev.UpSQL, "library_jobs_active") {
|
||||
t.Fatalf("migration 66 = %#v", prev)
|
||||
}
|
||||
if migrations[0].Version != 1 || !strings.Contains(migrations[0].UpSQL, "CREATE TABLE IF NOT EXISTS jobs") {
|
||||
t.Fatalf("migration 1 = %#v", migrations[0])
|
||||
|
||||
Reference in New Issue
Block a user