为什么:同步入口此前只会拒绝;授权、预备份、四类写入、验证与失败恢复都没有代码。
本轮把编排与授权实现出来,真实生产执行仍按授权闸口关闭。
- 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:
@@ -18,6 +18,7 @@ import (
|
||||
// 复用既有 fakeJobStore 提供 Store 的其余方法,只覆盖整库作业这几个。
|
||||
type fakeLibraryStore struct {
|
||||
fakeJobStore
|
||||
grant *governance.LibraryReleaseAuthorization
|
||||
versions []governance.LibraryVersion
|
||||
jobs []governance.LibraryJob
|
||||
createN int
|
||||
@@ -69,6 +70,32 @@ func (f *fakeLibraryStore) AdvanceLibraryJob(_ context.Context, _ string, state
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) CreateLibraryAuthorization(_ context.Context, item governance.LibraryReleaseAuthorization) error {
|
||||
f.grant = &item
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) ActiveLibraryAuthorization(_ context.Context, scope, snapshotID, digest string, now time.Time) (governance.LibraryReleaseAuthorization, bool, error) {
|
||||
if f.grant == nil || f.grant.Scope != scope || f.grant.SnapshotID != snapshotID || f.grant.ParamsDigest != digest {
|
||||
return governance.LibraryReleaseAuthorization{}, false, nil
|
||||
}
|
||||
if !f.grant.ValidAt(now) {
|
||||
return governance.LibraryReleaseAuthorization{}, false, nil
|
||||
}
|
||||
return *f.grant, true, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) LibraryAuthorizations(context.Context, string, int) ([]governance.LibraryReleaseAuthorization, error) {
|
||||
if f.grant == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return []governance.LibraryReleaseAuthorization{*f.grant}, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) RevokeLibraryAuthorization(context.Context, string, time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) AttachLibraryQueueJob(_ context.Context, _, _ string, _ time.Time) error {
|
||||
return nil
|
||||
}
|
||||
@@ -130,7 +157,7 @@ func TestLibrarySyncGateRequiresEveryPieceOfEvidence(t *testing.T) {
|
||||
}
|
||||
for _, testCase := range cases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
gate := librarySyncGate(testCase.config, testCase.production, testCase.prebackup, testCase.target)
|
||||
gate := librarySyncGateWithGrant(testCase.config, testCase.production, testCase.prebackup, testCase.target, true)
|
||||
if gate.SyncAllowed != testCase.allowed {
|
||||
t.Fatalf("sync_allowed = %v, want %v (%v)", gate.SyncAllowed, testCase.allowed, gate.Reasons)
|
||||
}
|
||||
@@ -194,6 +221,54 @@ func TestLibrarySyncEnqueueRequiresPrebackupForTarget(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 证据齐全 + 有效授权时才会真正建作业并入队;缺授权一律拒绝且不建作业。
|
||||
func TestLibrarySyncEnqueueCreatesJobOnlyWithGrant(t *testing.T) {
|
||||
target := "snap-1"
|
||||
digest, err := librarySyncParamsDigest("internal", target)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := &fakeLibraryStore{versions: []governance.LibraryVersion{
|
||||
{ID: "ver-prod", Scope: "internal", Kind: governance.LibraryVersionProductionEffective,
|
||||
SnapshotID: "prod-snap", VersionLabel: "prod-2026.10", CreatedAt: time.Now().UTC()},
|
||||
{ID: "ver-backup", Scope: "internal", Kind: governance.LibraryVersionProductionPrebackup,
|
||||
SnapshotID: target, CreatedAt: time.Now().UTC()},
|
||||
}}
|
||||
store.enqueueEchoRequestedID = true
|
||||
config := Config{Store: store, ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: true,
|
||||
ProductionWeKnoraBaseURL: "http://prod.invalid", LibraryCapacityBudgetBytes: 1 << 40}
|
||||
body := `{"scope":"internal","target_snapshot_id":"` + target + `"}`
|
||||
|
||||
// 先没有授权
|
||||
recorder := httptest.NewRecorder()
|
||||
librarySyncEnqueue(recorder, httptest.NewRequest(http.MethodPost, "/api/v1/library/syncs", strings.NewReader(body)), config)
|
||||
if recorder.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("without grant status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if store.createN != 0 {
|
||||
t.Fatal("must not create a job without a grant")
|
||||
}
|
||||
|
||||
// 补上绑定同一目标与快照的授权
|
||||
now := time.Now().UTC()
|
||||
store.grant = &governance.LibraryReleaseAuthorization{ID: "auth-1", Scope: "internal", SnapshotID: target,
|
||||
ParamsDigest: digest, AuthorizedBy: "admin", ExpiresAt: now.Add(30 * time.Minute), CreatedAt: now}
|
||||
recorder = httptest.NewRecorder()
|
||||
librarySyncEnqueue(recorder, httptest.NewRequest(http.MethodPost, "/api/v1/library/syncs", strings.NewReader(body)), config)
|
||||
if recorder.Code != http.StatusAccepted {
|
||||
t.Fatalf("with grant status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if store.createN != 1 {
|
||||
t.Fatalf("created=%d, want 1", store.createN)
|
||||
}
|
||||
if store.enqueued.Type != "library.sync" {
|
||||
t.Fatalf("enqueued type=%q, want library.sync", store.enqueued.Type)
|
||||
}
|
||||
if !strings.Contains(string(store.enqueued.Payload), store.lastJob.ID) {
|
||||
t.Fatalf("queue payload must carry the library job id: %s", store.enqueued.Payload)
|
||||
}
|
||||
}
|
||||
|
||||
// 三类版本必须分开读:production_effective 才是生产当前,测试封存不能顶替。
|
||||
func TestLibraryOverviewKeepsVersionKindsSeparate(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
|
||||
Reference in New Issue
Block a user