diff --git a/cmd/libraryresidue/main.go b/cmd/libraryresidue/main.go index 164aaaa..0add89f 100644 --- a/cmd/libraryresidue/main.go +++ b/cmd/libraryresidue/main.go @@ -87,7 +87,7 @@ func run(ctx context.Context, config appconfig.Config, scope, snapshotID, backup if strings.TrimSpace(expectSource) != want { return nil, fmt.Errorf("来源范围与 -expect-source 不一致:归档为 %s,参数为 %s", want, expectSource) } - target, _, err := librarystack.OpenProductionStack(ctx, config, scope, backupSnapshotID, "") + target, _, err := librarystack.OpenProductionStack(ctx, config, scope, backupSnapshotID, "", nil) if err != nil { return nil, fmt.Errorf("装配目标失败: %w", err) } diff --git a/cmd/worker/library_handlers.go b/cmd/worker/library_handlers.go index e75d96a..bf4b17f 100644 --- a/cmd/worker/library_handlers.go +++ b/cmd/worker/library_handlers.go @@ -272,7 +272,7 @@ func runLibraryReleaseAttempt(ctx context.Context, writer *libraryAttemptWriter, // 生产索引映射基线:权威来源是当前生效版本(精确查),其次任何一次成功记录的映射。 // 只认"生效版本"会在恢复把版本顶替掉之后误判"索引被改动"。 knownMapping := knownIndexMapping(ctx, store, payload.Scope) - target, backups, wireErr := buildLibraryProductionStack(ctx, config, payload.Scope, payload.TargetSnapshotID, knownMapping) + target, backups, wireErr := buildLibraryProductionStack(ctx, config, restoreDispatchGate(config, store.Store), payload.Scope, payload.TargetSnapshotID, knownMapping) if wireErr == nil { if mapping, mapErr := target.Search().MappingSHA256(ctx); mapErr == nil { recorder.mappingDigest = mapping diff --git a/cmd/worker/library_recovery.go b/cmd/worker/library_recovery.go index 19c36ee..55ff8c6 100644 --- a/cmd/worker/library_recovery.go +++ b/cmd/worker/library_recovery.go @@ -114,7 +114,7 @@ func runLibraryRecovery(ctx context.Context, store *postgresstore.Store, config claim.RecoveryJobID, claim.RecoveryOperationID = job.ID, recoveryOp.ID ctx = objectgateway.WithClaim(ctx, claim) knownMapping := knownIndexMapping(ctx, store, payload.Scope) - target, _, wireErr := buildLibraryProductionStack(ctx, config, payload.Scope, prebackup, knownMapping) + target, _, wireErr := buildLibraryProductionStack(ctx, config, restoreDispatchGate(config, store), payload.Scope, prebackup, knownMapping) if wireErr != nil { return fmt.Errorf("恢复目标装配失败:%w", wireErr) } diff --git a/cmd/worker/library_restore_dispatch_wiring_test.go b/cmd/worker/library_restore_dispatch_wiring_test.go new file mode 100644 index 0000000..b62be04 --- /dev/null +++ b/cmd/worker/library_restore_dispatch_wiring_test.go @@ -0,0 +1,29 @@ +package main + +import ( + "testing" + + "knowledge-governance/internal/appconfig" + postgresstore "knowledge-governance/internal/platform/postgres" +) + +// A12:能力默认关闭;只有"配置开启 + 目标身份存在 + 真实治理台账"同时成立才装配。 +// 配置本身不是独占证明,baseline 装配永远不传该台账。 +func TestRestoreDispatchGateRequiresExplicitConfiguration(t *testing.T) { + store := &postgresstore.Store{} + identity := appconfig.Config{LibraryProductionDatabaseURL: "postgres://local@127.0.0.1:5432/target_db", + LibraryTargetInstance: "inst", LibraryProductionTenantID: 1, LibraryProductionInternalKB: "kb"} + if gate := restoreDispatchGate(identity, store); gate != nil { + t.Fatal("gate wired without the explicit switch") + } + enabled := identity + enabled.LibraryRestoreDispatchEnabled = true + if gate := restoreDispatchGate(enabled, store); gate == nil { + t.Fatal("gate not wired when explicitly enabled") + } + if gate := restoreDispatchGate(enabled, nil); gate != nil { + t.Fatal("gate wired without a governance store") + } + // 空目标配置仍会得到一个 gate:真实闸门是治理库激活登记(见 store 用例), + // 用"配置看起来完整"冒充独占证明才是要避免的。 +} diff --git a/cmd/worker/library_wiring.go b/cmd/worker/library_wiring.go index 7ef1e6c..f177a60 100644 --- a/cmd/worker/library_wiring.go +++ b/cmd/worker/library_wiring.go @@ -5,7 +5,9 @@ import ( "knowledge-governance/internal/appconfig" "knowledge-governance/internal/librarymaintenance" + "knowledge-governance/internal/libraryrelease" "knowledge-governance/internal/librarystack" + postgresstore "knowledge-governance/internal/platform/postgres" v071 "knowledge-governance/internal/releaseadapter/v071" ) @@ -18,13 +20,27 @@ func openLibraryMaintenance(config appconfig.Config) (*librarymaintenance.Manage return librarystack.OpenMaintenance(config) } +// restoreDispatchGate 只在部署显式开启时才把治理台账交给目标。 +// +// 为什么不能只信配置:配置为 true 只说明"允许装配",不证明旧 writer 已退出; +// 真正的发放许可还要求治理库里对该目标存在未撤销的激活登记(见迁移 78)。 +func restoreDispatchGate(config appconfig.Config, store *postgresstore.Store) libraryrelease.RestoreMarkerGate { + // 只判断"是否允许装配":目标身份与独占证明由治理台账的激活登记回答, + // 这里再用一个空配置也能算出来的摘要去当门槛只会制造虚假的严格。 + if !config.LibraryRestoreDispatchEnabled || store == nil { + return nil + } + return store +} + // buildLibraryProductionStack 用"要同步的这份快照"做契约装配目标与预备份存储。 -func buildLibraryProductionStack(ctx context.Context, config appconfig.Config, scope, snapshotID, knownMapping string) ( - *v071.ProductionLibraryTarget, *v071.ProductionBackupStore, error) { - return librarystack.OpenProductionStack(ctx, config, scope, snapshotID, knownMapping) +func buildLibraryProductionStack(ctx context.Context, config appconfig.Config, gate libraryrelease.RestoreMarkerGate, + scope, snapshotID, knownMapping string) (*v071.ProductionLibraryTarget, *v071.ProductionBackupStore, error) { + return librarystack.OpenProductionStack(ctx, config, scope, snapshotID, knownMapping, gate) } // buildLibraryBaselineStack 装配"给目标自己建基线"所需的目标与捕获器(契约取目标当前映射)。 +// 基线不参与发布/恢复派发,永远不传派发台账:只读/建基线不得获得恢复写许可。 func buildLibraryBaselineStack(ctx context.Context, config appconfig.Config, scope string) ( *v071.ProductionLibraryTarget, *v071.ProductionBackupStore, error) { return librarystack.OpenBaselineStack(ctx, config, scope) diff --git a/internal/appconfig/config.go b/internal/appconfig/config.go index cd071e6..ad256fd 100644 --- a/internal/appconfig/config.go +++ b/internal/appconfig/config.go @@ -23,8 +23,11 @@ type Config struct { ReleaseConfigFile string ProductionReleaseRequestsEnabled bool ProductionBackupRestoreVerified bool - ProductionReleaseAuthorizerRef string - ProductionReleaseAuthTokenFile string + // LibraryRestoreDispatchEnabled 允许 worker 装配 DS01 派发台账;默认 false。 + // 它不能单独证明独占:治理库里的目标激活登记同样必需。 + LibraryRestoreDispatchEnabled bool + ProductionReleaseAuthorizerRef string + ProductionReleaseAuthTokenFile string // Production WeKnora endpoint: the release target itself stays declared in // the release runtime config (RELEASE_CONFIG_FILE); these settings only let // the API probe that endpoint so the console can show its real state. @@ -304,6 +307,14 @@ func Load(getenv func(string) string) (Config, error) { return Config{}, fmt.Errorf("PRODUCTION_BACKUP_RESTORE_VERIFIED: %w", err) } } + // DS01 恢复标记派发协议:默认关闭。开关只是"允许装配"的意思,不是独占证明—— + // 真正发放许可还要求治理库里对该目标存在未撤销的激活登记。 + if raw := getenv("LIBRARY_RESTORE_DISPATCH_V2"); raw != "" { + config.LibraryRestoreDispatchEnabled, err = strconv.ParseBool(raw) + if err != nil { + return Config{}, fmt.Errorf("LIBRARY_RESTORE_DISPATCH_V2: %w", err) + } + } if raw := getenv("PRODUCTION_WEKNORA_ALLOW_DOCKER_HTTP"); raw != "" { config.ProductionWeKnoraAllowDockerHTTP, err = strconv.ParseBool(raw) if err != nil { diff --git a/internal/librarystack/stack.go b/internal/librarystack/stack.go index 70d9d94..07f987a 100644 --- a/internal/librarystack/stack.go +++ b/internal/librarystack/stack.go @@ -15,6 +15,7 @@ import ( "knowledge-governance/internal/appconfig" "knowledge-governance/internal/librarymaintenance" + "knowledge-governance/internal/libraryrelease" "knowledge-governance/internal/librarysnapshot" "knowledge-governance/internal/objectgateway" v071 "knowledge-governance/internal/releaseadapter/v071" @@ -89,7 +90,8 @@ func scopeConfigOf(config appconfig.Config, scope string) (scopeConfig, error) { }, nil } -func newTarget(ctx context.Context, config appconfig.Config, scope scopeConfig) (*v071.ProductionLibraryTarget, error) { +func newTarget(ctx context.Context, config appconfig.Config, scope scopeConfig, + gate libraryrelease.RestoreMarkerGate) (*v071.ProductionLibraryTarget, error) { objects, err := v071.NewS3ObjectStore(scope.s3) if err != nil { return nil, errors.New("生产对象存储配置无效") @@ -110,12 +112,18 @@ func newTarget(ctx context.Context, config appconfig.Config, scope scopeConfig) } writer = gateway } + // DS01:派发身份只能来自本次真实 attempt(objectgateway claim)与原窗口作业。 + // gate 为 nil 时该能力整体关闭,adapter 对新续作 fail closed;baseline 永远传 nil。 + claim := objectgateway.ClaimFromContext(ctx) return v071.NewProductionLibraryTarget(ctx, v071.ProductionLibraryConfig{ Postgres: config.LibraryProductionDatabaseURL, SearchURL: config.LibraryProductionElasticURL, SearchIndex: config.LibraryProductionIndex, Objects: writer, ObjectsReader: objects, - ArchiveRoot: config.LibraryArchiveRoot, - ObjectsBucket: config.LibraryProductionS3Bucket, - MaxDocuments: 200000, + ArchiveRoot: config.LibraryArchiveRoot, + ObjectsBucket: config.LibraryProductionS3Bucket, + MaxDocuments: 200000, + RestoreDispatch: gate, RestoreTargetIdentity: config.LibraryTargetIdentity(), + RestoreOperationID: claim.OperationID, RestoreWindowJobID: claim.JobID, + RestoreAttemptOwner: claim.Owner, RestoreAttemptEpoch: claim.AttemptEpoch, }, scope.target) } @@ -130,13 +138,13 @@ func newBackups(config appconfig.Config, scope string, target *v071.ProductionLi // OpenProductionStack 用部署注入的配置装配目标与独立预备份存储。 // 契约以"要同步的这份快照"为准:生产必须向它看齐,而不是反过来。 -func OpenProductionStack(ctx context.Context, config appconfig.Config, scope, snapshotID, knownMapping string) ( - *v071.ProductionLibraryTarget, *v071.ProductionBackupStore, error) { +func OpenProductionStack(ctx context.Context, config appconfig.Config, scope, snapshotID, knownMapping string, + gate libraryrelease.RestoreMarkerGate) (*v071.ProductionLibraryTarget, *v071.ProductionBackupStore, error) { scopeCfg, err := scopeConfigOf(config, scope) if err != nil { return nil, nil, err } - target, err := newTarget(ctx, config, scopeCfg) + target, err := newTarget(ctx, config, scopeCfg, gate) if err != nil { return nil, nil, err } @@ -218,7 +226,7 @@ func OpenBaselineStack(ctx context.Context, config appconfig.Config, scope strin if err != nil { return nil, nil, err } - target, err := newTarget(ctx, config, scopeCfg) + target, err := newTarget(ctx, config, scopeCfg, nil) if err != nil { return nil, nil, err }