package v071 import ( "context" "crypto/rand" "encoding/hex" "errors" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "knowledge-governance/internal/governance" "knowledge-governance/internal/libraryrelease" "knowledge-governance/internal/objectgateway" postgresstore "knowledge-governance/internal/platform/postgres" "knowledge-governance/internal/platform/postgres/pgtest" "net/http/httptest" "net/url" "os" "sync/atomic" "testing" "time" ) type gatewayCountBackend struct { objectgateway.Backend puts atomic.Int64 } func (b *gatewayCountBackend) Put(ctx context.Context, key string, body []byte, owner string) (objectgateway.Version, error) { b.puts.Add(1) return b.Backend.Put(ctx, key, body, owner) } func gatewayReadOnlyGovernance(t *testing.T, ctx context.Context, admin *pgxpool.Pool, dsn string) *pgxpool.Pool { t.Helper() random := make([]byte, 24) if _, e := rand.Read(random); e != nil { t.Fatal(e) } suffix := hex.EncodeToString(random) role := "kg_gateway_reader_" + suffix[:12] // This role is created only in the explicitly disposable fixture cluster. if _, e := admin.Exec(ctx, "CREATE ROLE "+pgx.Identifier{role}.Sanitize()+" LOGIN PASSWORD '"+suffix+"'"); e != nil { t.Fatal("create isolated reader role failed") } var schema string if admin.QueryRow(ctx, "SELECT current_schema()").Scan(&schema) != nil { t.Fatal("fixture schema") } t.Cleanup(func() { cleanup := context.Background() if _, e := admin.Exec(cleanup, "DROP OWNED BY "+pgx.Identifier{role}.Sanitize()); e != nil { t.Errorf("reader role privilege cleanup failed") } if _, e := admin.Exec(cleanup, "DROP ROLE "+pgx.Identifier{role}.Sanitize()); e != nil { t.Errorf("reader role cleanup failed") } }) if _, e := admin.Exec(ctx, "GRANT USAGE ON SCHEMA "+pgx.Identifier{schema}.Sanitize()+" TO "+pgx.Identifier{role}.Sanitize()); e != nil { t.Fatal("reader schema grant") } if _, e := admin.Exec(ctx, "GRANT SELECT ON library_jobs,library_operations,library_attempts,library_versions,jobs TO "+pgx.Identifier{role}.Sanitize()); e != nil { t.Fatal("reader table grant") } parsed, e := url.Parse(dsn) if e != nil { t.Fatal("fixture DSN") } parsed.User = url.UserPassword(role, suffix) config, e := pgxpool.ParseConfig(parsed.String()) if e != nil { t.Fatal("reader config") } config.ConnConfig.RuntimeParams["default_transaction_read_only"] = "on" pool, e := pgxpool.NewWithConfig(ctx, config) if e != nil { t.Fatal("reader connect") } t.Cleanup(pool.Close) t.Log("isolated SELECT-only governance role created; exact role cleanup registered:", role) return pool } // This is real authority/PG/HTTP/storage integration, not the formal worker // process test: no grant decorator or control credential is given to the target. func TestContractGatewayAuthorityFrozenPlanIsolated(t *testing.T) { fx := openWriteSetFixture(t) seedTargetData(t, fx) ctx := fx.ctx dsn := pgtest.IsolatedDatabaseURL(t, os.Getenv("KG_LIBRARY_TEST_DATABASE_URL")) governancePool, e := pgxpool.New(ctx, dsn) if e != nil { t.Fatal("governance pool") } t.Cleanup(governancePool.Close) if e = postgresstore.ApplyMigrations(ctx, governancePool); e != nil { t.Fatal(e) } if _, e = governancePool.Exec(ctx, objectgateway.Schema); e != nil { t.Fatal(e) } reader := gatewayReadOnlyGovernance(t, ctx, governancePool, dsn) const jobID = "gateway-authority-job" const targetID = "gateway-authority-target" store := postgresstore.NewStore(governancePool).WithLibraryTarget(targetID) snapshot := sealSourceSnapshot(t, fx, nil) now := time.Now().UTC() op := governance.LibraryOperation{ID: "gateway-authority-operation", Scope: "internal", Kind: governance.LibraryJobSync, TargetSnapshotID: snapshot, RequestDigest: "gateway-authority-params", OperationKey: "gateway-authority-key", CreatedAt: now} if _, _, e = store.EnsureLibraryOperation(ctx, op); e != nil { t.Fatal(e) } if _, _, e = store.CreateLibraryJob(ctx, governance.LibraryJob{ID: jobID, Scope: "internal", Kind: governance.LibraryJobSync, OperationID: op.ID, IdempotencyKey: jobID, RequestDigest: op.RequestDigest, TargetSnapshotID: snapshot}, governance.SyncStages(), now); e != nil { t.Fatal(e) } attempt, e := store.BeginLibraryAttempt(ctx, jobID, "worker-first", time.Hour, now) if e != nil { t.Fatal(e) } if _, e = governancePool.Exec(ctx, "UPDATE library_jobs SET state='running' WHERE id=$1", jobID); e != nil { t.Fatal(e) } authority := &GatewayGovernanceAuthority{Pool: reader, ArchiveRoot: fx.archiveRoot, TargetID: targetID, Provider: "minio", Bucket: fx.bucket} if e = authority.Ready(ctx); e != nil { t.Fatal(e) } privileged := *authority privileged.Pool = governancePool if privileged.Ready(ctx) == nil { t.Fatal("privileged controller reader accepted") } backend := &gatewayCountBackend{Backend: &objectgateway.S3Backend{Client: fx.objects.s3.client, Bucket: fx.bucket, MaxBytes: 1 << 20}} gateway := &objectgateway.Gateway{Pool: governancePool, Scope: fx.bucket, Backend: backend, Authority: authority, MaxBytes: 1 << 20} data := objectgateway.Digest([]byte("authority-data")) server := httptest.NewServer(objectgateway.HTTPServer{Gateway: gateway, DataToken: data, ControlToken: objectgateway.Digest([]byte("authority-control"))}) defer server.Close() client, e := objectgateway.NewClient(server.URL, data, fx.bucket) if e != nil { t.Fatal(e) } claim := objectgateway.Claim{JobID: jobID, OperationID: op.ID, AttemptEpoch: int64(attempt.Epoch), Owner: attempt.Owner} objects := &GatewayObjectStore{Client: client, Provider: "minio", Bucket: fx.bucket, Automatic: true, Claim: claim} target := fx.target(t) target.config.Objects = objects current, e := target.Current(ctx) if e != nil { t.Fatal(e) } backup, e := fx.backups(t, target).Prepare(ctx, snapshot, current) if e != nil { t.Fatal(e) } set, e := target.PlanWriteSet(ctx, libraryrelease.WriteSetPlan{JobID: jobID, Target: "internal", BackupSnapshotID: backup.SnapshotID, TargetSnapshotID: snapshot, Universe: backup.Universe}) if e != nil { t.Fatal(e) } claim.PlanDigest = set.Digest before := universeDump(t, fx.pool) if e = target.Apply(ctx, libraryrelease.StageDatabase, snapshot); e == nil { t.Fatal("unanchored plan permitted first PG write") } if diff := diffUniverse(before, universeDump(t, fx.pool)); diff != "" { t.Fatal("unanchored apply mutated PG", diff) } if e = store.RecordLibraryVersion(ctx, governance.LibraryVersion{ID: governance.LibraryVersionEventID(governance.LibraryVersionProductionPrebackup, "internal", backup.SnapshotID, op.ID), Scope: "internal", Kind: governance.LibraryVersionProductionPrebackup, SnapshotID: backup.SnapshotID, SourceJobID: jobID, OperationID: op.ID, AttemptEpoch: attempt.Epoch, AttemptOwner: attempt.Owner, CreatedAt: now, Evidence: map[string]any{"job": jobID, "backup_snapshot": backup.SnapshotID, "backup_bytes": backup.Bytes, "writeset_digest": set.Digest, "writeset_target_snapshot": snapshot}}); e != nil { t.Fatal(e) } for _, bad := range []objectgateway.Claim{{JobID: jobID, OperationID: op.ID, AttemptEpoch: claim.AttemptEpoch, Owner: "other-worker", PlanDigest: set.Digest}, {JobID: jobID, OperationID: op.ID, AttemptEpoch: claim.AttemptEpoch, Owner: claim.Owner, PlanDigest: objectgateway.Digest([]byte("replacement"))}} { if _, e = client.Acquire(ctx, bad); e == nil { t.Fatal("untrusted claim admitted") } } if e = target.Apply(ctx, libraryrelease.StageDatabase, snapshot); e != nil { t.Fatal(e) } if e = target.Apply(ctx, libraryrelease.StageResources, snapshot); e != nil { t.Fatal(e) } count := backend.puts.Load() if count == 0 { t.Fatal("no actual object send") } firstRequests := objects.requests if e = store.FinishLibraryAttempt(ctx, attempt, governance.LibraryAttemptFailed, "isolated takeover", time.Now().UTC()); e != nil { t.Fatal(e) } next, e := store.BeginLibraryAttempt(ctx, jobID, "worker-second", time.Hour, time.Now().UTC()) if e != nil { t.Fatal(e) } nextClaim := claim nextClaim.AttemptEpoch = int64(next.Epoch) nextClaim.Owner = next.Owner nextObjects := &GatewayObjectStore{Client: client, Provider: "minio", Bucket: fx.bucket, Automatic: true, Claim: nextClaim} nextTarget := fx.target(t) nextTarget.config.Objects = nextObjects if e = nextTarget.AdoptWriteSet(set); e != nil { t.Fatal(e) } if e = nextObjects.EnsureObjectGrant(ctx); e != nil { t.Fatal(e) } for key, original := range firstRequests { if nextObjects.requests[key] != original { t.Fatal("send identity replaced during new epoch", key) } } if nextObjects.authorization.Epoch <= objects.authorization.Epoch { t.Fatal("authorization did not advance") } if _, e = client.Acquire(ctx, claim); !errors.Is(e, objectgateway.ErrEpoch) { t.Fatal("old active owner admitted", e) } if e = nextTarget.Apply(ctx, libraryrelease.StageResources, snapshot); e != nil { t.Fatal(e) } if backend.puts.Load() != count { t.Fatal("resumed object send duplicated") } for _, obj := range set.Objects { if obj.Existing { continue } locator := obj.Locator observation, e := nextObjects.ObserveObject(ctx, obj.Provider, locator) if e != nil { t.Fatal(e) } if e = nextObjects.RemoveObject(ctx, obj.Provider, locator, observation); e != nil { t.Fatal(e) } versions, e := backend.List(ctx, obj.Key) if e != nil || len(versions) != 0 { t.Fatal("created object version residue", e) } } t.Logf("real readonly governance authority -> data-only HTTP acquisition -> PG/MinIO; first-PG anchor denial, new epoch unchanged send identity, PUTs=%d, original version exact cleanup", count) }