From 62fe55e474aa685143c2288386f50f2fe65e80ae Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 30 Sep 2026 03:34:02 +1000 Subject: [PATCH] feat(store): key control-plane resources by (org, kind) resources and resource_history are one document per org and kind, with per-org advisory locks, request-ID idempotency and audit keys. The control-plane Query/History/Command/Watch requests carry org_id; a request naming none acts in the default org until the caller's org is derived server-side. --- cmd/archie-state-store/recovery_test.go | 11 +- .../control_plane_watch_outage_test.go | 4 +- internal/app/archied/provider_secrets_test.go | 3 +- internal/app/archied/state_store_recovery.go | 4 +- .../state_store_recovery_postgres_test.go | 9 +- internal/app/controlplane/controlplane.go | 23 ++-- .../app/controlplane/controlplane_test.go | 48 +++++-- .../controlplane/controlplane_watch_test.go | 2 +- .../app/controlplane/model_settings_test.go | 2 +- internal/app/controlplane/registry.go | 8 +- internal/app/controlplane/registry_test.go | 28 +++-- internal/app/controlplane/runtime_config.go | 2 +- .../step_vocabulary_agreement_test.go | 3 +- .../app/controlplane/workflow_settings.go | 4 +- .../controlplane/v1/controlplane.pb.go | 67 ++++++++-- .../domain/storecontract/storecontract.go | 22 +++- .../migrations/0021_resources_per_org.sql | 24 ++++ .../postgres/postgresdb/audit.sql.go | 12 +- .../postgres/postgresdb/resources.sql.go | 91 ++++++++++---- .../infrastructure/postgres/queries/audit.sql | 4 +- .../postgres/queries/resources.sql | 36 +++--- internal/infrastructure/postgres/resources.go | 109 +++++++++------- .../postgres/statestore_test.go | 117 ++++++++++++++---- internal/webui/live_hub_test.go | 2 +- proto/controlplane/v1/controlplane.proto | 11 +- 25 files changed, 459 insertions(+), 187 deletions(-) create mode 100644 internal/infrastructure/postgres/migrations/0021_resources_per_org.sql diff --git a/cmd/archie-state-store/recovery_test.go b/cmd/archie-state-store/recovery_test.go index a75a39408..fcdcf7de1 100644 --- a/cmd/archie-state-store/recovery_test.go +++ b/cmd/archie-state-store/recovery_test.go @@ -259,7 +259,7 @@ func TestRecoveryRollbackReplaysTheRevisionThroughReplace(t *testing.T) { if !strings.Contains(stdout, controlplane.ModelRoleAssignmentsKind) { t.Errorf("rollback must name the resource it replayed; stdout = %q", stdout) } - resource, err := st.Resource(t.Context(), controlplane.ModelRoleAssignmentsKind) + resource, err := st.Resource(t.Context(), storecontract.DefaultOrgID, controlplane.ModelRoleAssignmentsKind) if err != nil { t.Fatal(err) } @@ -269,7 +269,7 @@ func TestRecoveryRollbackReplaysTheRevisionThroughReplace(t *testing.T) { if resource.Version != 3 { t.Errorf("resource version = %d, want a new revision (3) rather than a rewrite of the old one", resource.Version) } - history, err := st.ResourceHistory(t.Context(), controlplane.ModelRoleAssignmentsKind, 0) + history, err := st.ResourceHistory(t.Context(), storecontract.DefaultOrgID, controlplane.ModelRoleAssignmentsKind, 0) if err != nil { t.Fatal(err) } @@ -293,7 +293,7 @@ func TestRecoveryRollbackRestoresANamedRevision(t *testing.T) { if code != 0 { t.Fatalf("rollback exited %d: %s", code, stderr) } - resource, err := st.Resource(t.Context(), controlplane.ModelRoleAssignmentsKind) + resource, err := st.Resource(t.Context(), storecontract.DefaultOrgID, controlplane.ModelRoleAssignmentsKind) if err != nil { t.Fatal(err) } @@ -463,7 +463,7 @@ func storedResourceCount(t *testing.T, stdout string) int { // a silent no-op. func seedResource(t *testing.T, st *pgstore.TaskDB, kind, value, label string) { t.Helper() - current, err := st.Resource(t.Context(), kind) + current, err := st.Resource(t.Context(), storecontract.DefaultOrgID, kind) expected := int64(0) if err == nil { expected = current.Version @@ -477,7 +477,8 @@ func seedResource(t *testing.T, st *pgstore.TaskDB, kind, value, label string) { t.Fatal(err) } if _, err := st.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: kind, Value: encoded, Actor: label, Source: "test", + OrgID: storecontract.DefaultOrgID, + Kind: kind, Value: encoded, Actor: label, Source: "test", RequestID: fmt.Sprintf("%s-%d", label, expected+1), ExpectedVersion: expected, }); err != nil { t.Fatalf("seed resource %s: %v", kind, err) diff --git a/internal/app/archied/control_plane_watch_outage_test.go b/internal/app/archied/control_plane_watch_outage_test.go index 2e6998a8c..ce9ee7100 100644 --- a/internal/app/archied/control_plane_watch_outage_test.go +++ b/internal/app/archied/control_plane_watch_outage_test.go @@ -47,7 +47,7 @@ func (s *outageStore) down(stall time.Duration) { s.failing, s.stall = true, stall } -func (s *outageStore) Resource(ctx context.Context, kind string) (storecontract.Resource, error) { +func (s *outageStore) Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) { s.mu.Lock() failing, stall := s.failing, s.stall s.mu.Unlock() @@ -55,7 +55,7 @@ func (s *outageStore) Resource(ctx context.Context, kind string) (storecontract. time.Sleep(stall) return storecontract.Resource{}, errors.New("state store unreachable") } - return s.TaskDB.Resource(ctx, kind) + return s.TaskDB.Resource(ctx, orgID, kind) } // TestWatchRetryGrowsWhileTheStateStoreIsDown is the outage this reconnect diff --git a/internal/app/archied/provider_secrets_test.go b/internal/app/archied/provider_secrets_test.go index 6d6673140..0d8f48e99 100644 --- a/internal/app/archied/provider_secrets_test.go +++ b/internal/app/archied/provider_secrets_test.go @@ -13,6 +13,7 @@ import ( "github.com/samcharles93/archie-core/internal/app/controlplane" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" "github.com/samcharles93/archie-core/internal/secret" @@ -51,7 +52,7 @@ func TestStateStoreBootSeedsProviderSourceReference(t *testing.T) { go func() { errCh <- RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "127.0.0.1:0"}) }() deadline := time.After(10 * time.Second) for { - resource, err := store.Resource(t.Context(), controlplane.ProviderSettingsKind) + resource, err := store.Resource(t.Context(), storecontract.DefaultOrgID, controlplane.ProviderSettingsKind) if err == nil { if strings.Contains(string(resource.Value), "ARCHIE_PROVIDER_") || !strings.Contains(string(resource.Value), ref.Key) { t.Fatalf("stored provider settings = %s, want source reference and no derived env name", resource.Value) diff --git a/internal/app/archied/state_store_recovery.go b/internal/app/archied/state_store_recovery.go index 738ae9a47..a4d448a3e 100644 --- a/internal/app/archied/state_store_recovery.go +++ b/internal/app/archied/state_store_recovery.go @@ -135,7 +135,7 @@ func replayRevision(ctx context.Context, resources controlplane.ResourceStore, o if !server.Owns(options.Kind) { return "", fmt.Errorf("unknown resource kind %q", options.Kind) } - current, err := resources.Resource(ctx, options.Kind) + current, err := resources.Resource(ctx, storecontract.DefaultOrgID, options.Kind) if errors.Is(err, storecontract.ErrResourceNotFound) { return "", fmt.Errorf("%s has no stored value to roll back", options.Kind) } @@ -165,7 +165,7 @@ func replayRevision(ctx context.Context, resources controlplane.ResourceStore, o // revisionToReplay picks the value to put back: the revision the operator // named, or the newest one older than the value the resource carries now. func revisionToReplay(ctx context.Context, st controlplane.ResourceStore, kind string, current, requested int64) (storecontract.Resource, error) { - history, err := st.ResourceHistory(ctx, kind, 0) + history, err := st.ResourceHistory(ctx, storecontract.DefaultOrgID, kind, 0) if err != nil { return storecontract.Resource{}, fmt.Errorf("read %s history: %w", kind, err) } diff --git a/internal/app/archied/state_store_recovery_postgres_test.go b/internal/app/archied/state_store_recovery_postgres_test.go index 082c52c0d..828ce442b 100644 --- a/internal/app/archied/state_store_recovery_postgres_test.go +++ b/internal/app/archied/state_store_recovery_postgres_test.go @@ -54,11 +54,12 @@ func putResource(t *testing.T, pool *pgxpool.Pool, kind, value, label string) { t.Helper() resources := postgres.NewResources(pool) expected := int64(0) - if current, err := resources.Resource(t.Context(), kind); err == nil { + if current, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, kind); err == nil { expected = current.Version } if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: kind, Value: []byte(value), Actor: label, Source: "test", + OrgID: storecontract.DefaultOrgID, + Kind: kind, Value: []byte(value), Actor: label, Source: "test", RequestID: fmt.Sprintf("%s-%d", label, expected+1), ExpectedVersion: expected, }); err != nil { t.Fatalf("put %s: %v", kind, err) @@ -125,7 +126,7 @@ func TestPostgresRecoveryBackupAndRestore(t *testing.T) { if !strings.Contains(summary, "after the snapshot") { t.Errorf("restore must say writes after the snapshot are lost; summary = %q", summary) } - current, err := postgres.NewResources(pool).Resource(t.Context(), controlplane.ModelRoleAssignmentsKind) + current, err := postgres.NewResources(pool).Resource(t.Context(), storecontract.DefaultOrgID, controlplane.ModelRoleAssignmentsKind) if err != nil { t.Fatal(err) } @@ -188,7 +189,7 @@ func TestPostgresRecoveryRollback(t *testing.T) { if _, err := RunStateStoreRecovery(t.Context(), options); err != nil { t.Fatalf("rollback: %v", err) } - current, err := postgres.NewResources(pool).Resource(t.Context(), kind) + current, err := postgres.NewResources(pool).Resource(t.Context(), storecontract.DefaultOrgID, kind) if err != nil { t.Fatal(err) } diff --git a/internal/app/controlplane/controlplane.go b/internal/app/controlplane/controlplane.go index b4f3199b3..f76b70296 100644 --- a/internal/app/controlplane/controlplane.go +++ b/internal/app/controlplane/controlplane.go @@ -31,8 +31,8 @@ var ( // root asserts the State Store against it, so this is the single definition of // what a control-plane backing store must offer. type ResourceStore interface { - Resource(context.Context, string) (storecontract.Resource, error) - ResourceHistory(context.Context, string, int) ([]storecontract.Resource, error) + Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) + ResourceHistory(ctx context.Context, orgID, kind string, limit int) ([]storecontract.Resource, error) Audit(ctx context.Context, table string, keys []string, limit int) ([]storecontract.AuditEntry, error) PutResource(context.Context, storecontract.ResourceWrite) (storecontract.Resource, error) } @@ -78,7 +78,7 @@ func (s *Server) Query(ctx context.Context, request *pb.QueryRequest) (*pb.Query if _, ok := s.definitions[request.GetKind()]; !ok { return nil, status.Error(codes.NotFound, "resource not found") } - resource, err := s.store.Resource(ctx, request.Kind) + resource, err := s.store.Resource(ctx, requestOrg(request.GetOrgId()), request.Kind) if err != nil { return nil, mapError(err) } @@ -96,7 +96,7 @@ func (s *Server) History(ctx context.Context, request *pb.HistoryRequest) (*pb.H if limit <= 0 || limit > defaultHistoryLimit { limit = defaultHistoryLimit } - revisions, err := s.store.ResourceHistory(ctx, request.Kind, limit) + revisions, err := s.store.ResourceHistory(ctx, requestOrg(request.GetOrgId()), request.Kind, limit) if err != nil { return nil, mapError(err) } @@ -149,7 +149,7 @@ func (s *Server) Command(ctx context.Context, request *pb.CommandRequest) (*pb.C if err != nil { return nil, mapError(err) } - resource, err := s.store.PutResource(ctx, storecontract.ResourceWrite{Kind: request.Kind, Value: value, Actor: request.Actor, Source: request.Source, RequestID: request.RequestId, ExpectedVersion: request.ExpectedVersion, At: time.Now().UTC()}) + resource, err := s.store.PutResource(ctx, storecontract.ResourceWrite{OrgID: requestOrg(request.GetOrgId()), Kind: request.Kind, Value: value, Actor: request.Actor, Source: request.Source, RequestID: request.RequestId, ExpectedVersion: request.ExpectedVersion, At: time.Now().UTC()}) if err != nil { return nil, mapError(err) } @@ -164,7 +164,7 @@ func (s *Server) Watch(request *pb.WatchRequest, stream pb.ControlPlaneService_W ticker := time.NewTicker(250 * time.Millisecond) defer ticker.Stop() for { - resource, err := s.store.Resource(stream.Context(), request.Kind) + resource, err := s.store.Resource(stream.Context(), requestOrg(request.GetOrgId()), request.Kind) if err == nil && resource.Version > version { if err := stream.Send(&pb.WatchResponse{Resource: resourceProto(resource)}); err != nil { return err @@ -182,7 +182,16 @@ func (s *Server) Watch(request *pb.WatchRequest, stream pb.ControlPlaneService_W } func resourceProto(resource storecontract.Resource) *pb.Resource { - return &pb.Resource{Kind: resource.Kind, Version: resource.Version, ValueJson: resource.Value, UpdatedAt: timestamp(resource.At)} + return &pb.Resource{OrgId: resource.OrgID, Kind: resource.Kind, Version: resource.Version, ValueJson: resource.Value, UpdatedAt: timestamp(resource.At)} +} + +// requestOrg is the org a request acts in. Until the caller's org is derived +// server-side (access), a request that names none acts in the default org. +func requestOrg(orgID string) string { + if orgID == "" { + return storecontract.DefaultOrgID + } + return orgID } func timestamp(value time.Time) *timestamppb.Timestamp { diff --git a/internal/app/controlplane/controlplane_test.go b/internal/app/controlplane/controlplane_test.go index a73928322..51484718b 100644 --- a/internal/app/controlplane/controlplane_test.go +++ b/internal/app/controlplane/controlplane_test.go @@ -79,7 +79,7 @@ func TestImportConfigSeedsWorkflowDefinitionsWithoutOverwritingOverride(t *testi if _, _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil { t.Fatal(err) } - resource, err := resources.Resource(t.Context(), WorkflowDefinitionsKind) + resource, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind) if err != nil { t.Fatal(err) } @@ -95,13 +95,13 @@ func TestImportConfigSeedsWorkflowDefinitionsWithoutOverwritingOverride(t *testi if err != nil { t.Fatal(err) } - if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{Kind: WorkflowDefinitionsKind, Value: value, ExpectedVersion: resource.Version, Actor: "test", Source: "test", RequestID: "override"}); err != nil { + if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{OrgID: storecontract.DefaultOrgID, Kind: WorkflowDefinitionsKind, Value: value, ExpectedVersion: resource.Version, Actor: "test", Source: "test", RequestID: "override"}); err != nil { t.Fatal(err) } if _, _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil { t.Fatal(err) } - got, err := resources.Resource(t.Context(), WorkflowDefinitionsKind) + got, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind) if err != nil { t.Fatal(err) } @@ -119,7 +119,7 @@ func TestImportConfigSeedsPersonasWithoutOverwritingEdits(t *testing.T) { if _, _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil { t.Fatal(err) } - resource, err := resources.Resource(t.Context(), PersonasKind) + resource, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, PersonasKind) if err != nil { t.Fatal(err) } @@ -132,13 +132,13 @@ func TestImportConfigSeedsPersonasWithoutOverwritingEdits(t *testing.T) { if err != nil { t.Fatal(err) } - if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{Kind: PersonasKind, Value: value, ExpectedVersion: resource.Version, Actor: "test", Source: "test", RequestID: "persona-edit"}); err != nil { + if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{OrgID: storecontract.DefaultOrgID, Kind: PersonasKind, Value: value, ExpectedVersion: resource.Version, Actor: "test", Source: "test", RequestID: "persona-edit"}); err != nil { t.Fatal(err) } if _, _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil { t.Fatal(err) } - got, err := resources.Resource(t.Context(), PersonasKind) + got, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, PersonasKind) if err != nil { t.Fatal(err) } @@ -161,7 +161,7 @@ func TestImportWorkflowExecutionSettingsDoesNotOverwriteExistingValue(t *testing if _, err := server.ImportWorkflowExecutionSettings(t.Context(), workflow.ExecutionSettings{MaxModelToolSteps: 99, MaxRuntime: time.Hour, MaxConsecutiveGateFailures: 9}); err != nil { t.Fatal(err) } - resource, err := resources.Resource(t.Context(), WorkflowExecutionSettingsKind) + resource, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, WorkflowExecutionSettingsKind) if err != nil { t.Fatal(err) } @@ -186,7 +186,7 @@ func TestRegistryRoutesAndValidatesDefinitions(t *testing.T) { if _, _, err := server.ImportConfig(t.Context(), config.Config{Containers: config.ContainerConfig{Image: "archie-agent:test", PullPolicy: "missing"}}); err != nil { t.Fatal(err) } - if _, err := resources.Resource(t.Context(), ContainerRuntimePoliciesKind); err != nil { + if _, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, ContainerRuntimePoliciesKind); err != nil { t.Fatalf("container runtime policies were not seeded: %v", err) } @@ -220,7 +220,7 @@ func TestProviderSeedKeepsReferencesAndNeverResolvedSecrets(t *testing.T) { if _, _, err := server.ImportConfig(context.Background(), cfg); err != nil { t.Fatal(err) } - resource, err := resources.Resource(t.Context(), ProviderSettingsKind) + resource, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, ProviderSettingsKind) if err != nil { t.Fatal(err) } @@ -308,12 +308,38 @@ func TestImportConfigSkipsASeedItCannotValidate(t *testing.T) { if len(skipped) != 1 || skipped[0].Kind != RepositoryPoliciesKind || skipped[0].Err == nil { t.Fatalf("skipped = %+v, want the repository policies and the reason", skipped) } - if _, err := resources.Resource(t.Context(), RepositoryPoliciesKind); !errors.Is(err, storecontract.ErrResourceNotFound) { + if _, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, RepositoryPoliciesKind); !errors.Is(err, storecontract.ErrResourceNotFound) { t.Errorf("repository policies Resource = %v, want ErrResourceNotFound: an invalid seed must not be stored", err) } // Kinds that are fine are still seeded: one skipped kind does not abandon // the rest of the migration. - if _, err := resources.Resource(t.Context(), WorkflowDefinitionsKind); err != nil { + if _, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind); err != nil { t.Errorf("workflow definitions: %v, want them seeded alongside the skipped kind", err) } } + +// A request names the org whose copy of a kind it acts on; one naming none acts +// in the default org, so a write for one org is invisible to the others. +func TestControlPlaneRequestsActInTheirOrg(t *testing.T) { + t.Parallel() + + resources := pgstore.Open(t) + defer resources.Close() + server := testServer(t, resources) + written, err := server.Command(t.Context(), &pb.CommandRequest{ + Kind: WorkflowEnablementKind, Command: "replace", ValueJson: []byte(`{"orgs":{}}`), + Actor: "test", Source: "test", RequestId: "acme-write", OrgId: "acme", + }) + if err != nil { + t.Fatal(err) + } + if written.Resource.OrgId != "acme" || written.Resource.Version != 1 { + t.Fatalf("written = %+v, want acme at version 1", written.Resource) + } + if _, err := server.Query(t.Context(), &pb.QueryRequest{Kind: WorkflowEnablementKind, OrgId: "acme"}); err != nil { + t.Fatalf("acme Query: %v", err) + } + if _, err := server.Query(t.Context(), &pb.QueryRequest{Kind: WorkflowEnablementKind}); status.Code(err) != codes.NotFound { + t.Fatalf("default-org Query = %v, want NotFound: acme's write leaked into the default org", err) + } +} diff --git a/internal/app/controlplane/controlplane_watch_test.go b/internal/app/controlplane/controlplane_watch_test.go index 48edc89b9..ab6ded7db 100644 --- a/internal/app/controlplane/controlplane_watch_test.go +++ b/internal/app/controlplane/controlplane_watch_test.go @@ -285,7 +285,7 @@ func TestWatchServesOnlyVersionsAfterTheRequestedOne(t *testing.T) { if err != nil { t.Fatal(err) } - if _, err := resources.PutResource(ctx, storecontract.ResourceWrite{Kind: WorkflowExecutionSettingsKind, Value: value, ExpectedVersion: version, Actor: "test", Source: "test", RequestID: "watch-next"}); err != nil { + if _, err := resources.PutResource(ctx, storecontract.ResourceWrite{OrgID: storecontract.DefaultOrgID, Kind: WorkflowExecutionSettingsKind, Value: value, ExpectedVersion: version, Actor: "test", Source: "test", RequestID: "watch-next"}); err != nil { t.Fatal(err) } select { diff --git a/internal/app/controlplane/model_settings_test.go b/internal/app/controlplane/model_settings_test.go index 41ba5e83a..a2901dac3 100644 --- a/internal/app/controlplane/model_settings_test.go +++ b/internal/app/controlplane/model_settings_test.go @@ -45,7 +45,7 @@ func TestImportConfigRefusesAResolvedProviderSnapshot(t *testing.T) { if !reported { t.Errorf("skipped = %+v, want provider-settings reported", skipped) } - if _, err := resources.Resource(t.Context(), ProviderSettingsKind); err == nil { + if _, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, ProviderSettingsKind); err == nil { t.Fatal("provider-settings was stored from a resolved snapshot") } else if !strings.Contains(err.Error(), "not found") { t.Fatalf("Resource = %v, want %v", err, storecontract.ErrResourceNotFound) diff --git a/internal/app/controlplane/registry.go b/internal/app/controlplane/registry.go index eb2b473c1..6f428911a 100644 --- a/internal/app/controlplane/registry.go +++ b/internal/app/controlplane/registry.go @@ -156,7 +156,7 @@ func (r *seedRefusal) Unwrap() error { return r.err } // value this build derived is not one the resource's validator accepts, and // nothing was written. func (s *Server) seedKind(ctx context.Context, definition Definition, cfg config.Config) (int64, error) { - stored, storedErr := s.store.Resource(ctx, definition.Kind) + stored, storedErr := s.store.Resource(ctx, storecontract.DefaultOrgID, definition.Kind) absent := errors.Is(storedErr, storecontract.ErrResourceNotFound) if storedErr != nil && !absent { return 0, storedErr @@ -188,7 +188,7 @@ func (s *Server) seedKind(ctx context.Context, definition Definition, cfg config } } write := storecontract.ResourceWrite{ - Kind: definition.Kind, Value: value, + OrgID: storecontract.DefaultOrgID, Kind: definition.Kind, Value: value, Actor: seedActor, Source: seedSource, RequestID: seedRequestID(definition.Kind, value), ExpectedVersion: expectedVersion(stored, absent), At: time.Now().UTC(), @@ -226,7 +226,7 @@ func (s *Server) shippedValueIsStale(ctx context.Context, definition Definition) // seed. It reads the ledger rather than the current row because the ledger is // what records who wrote a value, and the row does not. func (s *Server) seedWroteNewest(ctx context.Context, kind string) (bool, error) { - history, err := s.store.ResourceHistory(ctx, kind, 1) + history, err := s.store.ResourceHistory(ctx, storecontract.DefaultOrgID, kind, 1) if err != nil { return false, fmt.Errorf("read %s history: %w", kind, err) } @@ -305,7 +305,7 @@ func (s *Server) ValidateStored(ctx context.Context) (int, error) { checked := 0 var failures []error for _, definition := range s.ordered { - resource, err := s.store.Resource(ctx, definition.Kind) + resource, err := s.store.Resource(ctx, storecontract.DefaultOrgID, definition.Kind) if errors.Is(err, storecontract.ErrResourceNotFound) { continue } diff --git a/internal/app/controlplane/registry_test.go b/internal/app/controlplane/registry_test.go index 91738b113..71288dd9c 100644 --- a/internal/app/controlplane/registry_test.go +++ b/internal/app/controlplane/registry_test.go @@ -46,13 +46,14 @@ func staleWorkflowDefinitions(t *testing.T) []byte { // -- so a test does not restate a key or an identity it would then be pinning. func writeSeeded(t *testing.T, resources *pgstore.TaskDB, kind string, value []byte) storecontract.Resource { t.Helper() - if current, err := resources.Resource(t.Context(), kind); err == nil { + if current, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, kind); err == nil { t.Fatalf("%s already holds version %d; a seeded fixture writes the first revision", kind, current.Version) } else if !errors.Is(err, storecontract.ErrResourceNotFound) { t.Fatalf("read %s: %v", kind, err) } seeded, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: kind, Value: value, ExpectedVersion: 0, + OrgID: storecontract.DefaultOrgID, + Kind: kind, Value: value, ExpectedVersion: 0, Actor: seedActor, Source: seedSource, RequestID: seedRequestID(kind, value), }) if err != nil { @@ -66,7 +67,8 @@ func writeSeeded(t *testing.T, resources *pgstore.TaskDB, kind string, value []b func replaceAsOperator(t *testing.T, resources *pgstore.TaskDB, kind string, value []byte, expected int64) storecontract.Resource { t.Helper() replaced, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: kind, Value: value, ExpectedVersion: expected, + OrgID: storecontract.DefaultOrgID, + Kind: kind, Value: value, ExpectedVersion: expected, Actor: "operator", Source: "archie-ui", RequestID: "operator-replace-" + kind, }) if err != nil { @@ -96,7 +98,7 @@ func TestImportConfigRefreshesAStaleShippedValue(t *testing.T) { if err != nil { t.Fatalf("ImportConfig: %v", err) } - refreshed, err := resources.Resource(t.Context(), WorkflowDefinitionsKind) + refreshed, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind) if err != nil { t.Fatal(err) } @@ -115,7 +117,7 @@ func TestImportConfigRefreshesAStaleShippedValue(t *testing.T) { } // A refresh is a write like any other: what it replaced stays a revision, so // the value the previous release shipped is still readable. - history, err := resources.ResourceHistory(t.Context(), WorkflowDefinitionsKind, 0) + history, err := resources.ResourceHistory(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind, 0) if err != nil { t.Fatal(err) } @@ -138,7 +140,7 @@ func TestImportConfigLeavesASeededValueAloneOnceItMatches(t *testing.T) { if _, _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil { t.Fatalf("ImportConfig %d: %v", attempt, err) } - stored, err := resources.Resource(t.Context(), WorkflowDefinitionsKind) + stored, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind) if err != nil { t.Fatal(err) } @@ -220,7 +222,7 @@ func TestImportConfigKeepsAStoredValueThatIsNotTheSeeds(t *testing.T) { if _, _, err := testServer(t, resources).ImportConfig(t.Context(), resolved); err != nil { t.Fatal(err) } - stored, err := resources.Resource(t.Context(), RepositoryPoliciesKind) + stored, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, RepositoryPoliciesKind) if err != nil { t.Fatal(err) } @@ -243,7 +245,7 @@ func TestImportConfigKeepsAStoredValueThatIsNotTheSeeds(t *testing.T) { defer resources.Close() server := testServer(t, resources) want := tt.settle(t, resources) - before, err := resources.Resource(t.Context(), tt.kind) + before, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, tt.kind) if err != nil { t.Fatal(err) } @@ -253,7 +255,7 @@ func TestImportConfigKeepsAStoredValueThatIsNotTheSeeds(t *testing.T) { if err != nil { t.Fatalf("ImportConfig %d: %v", attempt, err) } - after, err := resources.Resource(t.Context(), tt.kind) + after, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, tt.kind) if err != nil { t.Fatal(err) } @@ -284,11 +286,11 @@ func TestImportConfigRecreatesAKindAnOperatorRemoved(t *testing.T) { if _, _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil { t.Fatal(err) } - seeded, err := resources.Resource(t.Context(), WorkflowDefinitionsKind) + seeded, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind) if err != nil { t.Fatal(err) } - history, err := resources.ResourceHistory(t.Context(), WorkflowDefinitionsKind, 0) + history, err := resources.ResourceHistory(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind, 0) if err != nil { t.Fatal(err) } @@ -299,14 +301,14 @@ func TestImportConfigRecreatesAKindAnOperatorRemoved(t *testing.T) { if _, err := resources.Pool.Exec(t.Context(), `DELETE FROM resources WHERE kind = $1`, WorkflowDefinitionsKind); err != nil { t.Fatalf("remove the resource: %v", err) } - if _, err := resources.Resource(t.Context(), WorkflowDefinitionsKind); !errors.Is(err, storecontract.ErrResourceNotFound) { + if _, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind); !errors.Is(err, storecontract.ErrResourceNotFound) { t.Fatalf("resource after the removal = %v, want ErrResourceNotFound", err) } if _, _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil { t.Fatalf("ImportConfig after the removal: %v", err) } - restored, err := resources.Resource(t.Context(), WorkflowDefinitionsKind) + restored, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind) if err != nil { t.Fatalf("the removed %s was not re-seeded: %v", WorkflowDefinitionsKind, err) } diff --git a/internal/app/controlplane/runtime_config.go b/internal/app/controlplane/runtime_config.go index 2abe0b8c4..e5222ae22 100644 --- a/internal/app/controlplane/runtime_config.go +++ b/internal/app/controlplane/runtime_config.go @@ -71,7 +71,7 @@ type storeReader struct { } func (r storeReader) Query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error) { - resource, err := r.resources.Resource(ctx, kind) + resource, err := r.resources.Resource(ctx, storecontract.DefaultOrgID, kind) if errors.Is(err, storecontract.ErrResourceNotFound) { seed, ok := r.seeds[kind] if !ok { diff --git a/internal/app/controlplane/step_vocabulary_agreement_test.go b/internal/app/controlplane/step_vocabulary_agreement_test.go index 9160cc36f..be690c038 100644 --- a/internal/app/controlplane/step_vocabulary_agreement_test.go +++ b/internal/app/controlplane/step_vocabulary_agreement_test.go @@ -34,6 +34,7 @@ import ( "github.com/samcharles93/archie-core/internal/app/agentworker" pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" "github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps" @@ -267,7 +268,7 @@ func admitThroughStateStore(t *testing.T, steps *workflow.Manager, stepType stri t.Fatalf("build control plane server: %v", err) } - if resource, resourceErr := resources.Resource(t.Context(), WorkflowDefinitionsKind); resourceErr == nil { + if resource, resourceErr := resources.Resource(t.Context(), storecontract.DefaultOrgID, WorkflowDefinitionsKind); resourceErr == nil { before = resource.Version } value, err := json.Marshal(probeCollection(stepType)) diff --git a/internal/app/controlplane/workflow_settings.go b/internal/app/controlplane/workflow_settings.go index cf521d545..772bae9b2 100644 --- a/internal/app/controlplane/workflow_settings.go +++ b/internal/app/controlplane/workflow_settings.go @@ -33,7 +33,7 @@ type executionSettingsDocument struct { // holds is left alone, and a kind it does not hold is seeded under the one seed // identity (see seedActor and seedRequestID). func (s *Server) ImportWorkflowExecutionSettings(ctx context.Context, settings workflow.ExecutionSettings) (int64, error) { - resource, err := s.store.Resource(ctx, WorkflowExecutionSettingsKind) + resource, err := s.store.Resource(ctx, storecontract.DefaultOrgID, WorkflowExecutionSettingsKind) if err == nil { return resource.Version, nil } @@ -48,7 +48,7 @@ func (s *Server) ImportWorkflowExecutionSettings(ctx context.Context, settings w if err != nil { return 0, err } - resource, err = s.store.PutResource(ctx, storecontract.ResourceWrite{Kind: WorkflowExecutionSettingsKind, Value: value, Actor: seedActor, Source: seedSource, RequestID: seedRequestID(WorkflowExecutionSettingsKind, value), ExpectedVersion: 0, At: time.Now().UTC()}) + resource, err = s.store.PutResource(ctx, storecontract.ResourceWrite{OrgID: storecontract.DefaultOrgID, Kind: WorkflowExecutionSettingsKind, Value: value, Actor: seedActor, Source: seedSource, RequestID: seedRequestID(WorkflowExecutionSettingsKind, value), ExpectedVersion: 0, At: time.Now().UTC()}) return resource.Version, err } diff --git a/internal/contracts/controlplane/v1/controlplane.pb.go b/internal/contracts/controlplane/v1/controlplane.pb.go index 087457349..852f666fb 100644 --- a/internal/contracts/controlplane/v1/controlplane.pb.go +++ b/internal/contracts/controlplane/v1/controlplane.pb.go @@ -213,6 +213,7 @@ type Resource struct { Version int64 `protobuf:"varint,2,opt,name=version,proto3" json:"version,omitempty"` ValueJson []byte `protobuf:"bytes,3,opt,name=value_json,json=valueJson,proto3" json:"value_json,omitempty"` UpdatedAt *timestamppb.Timestamp `protobuf:"bytes,4,opt,name=updated_at,json=updatedAt,proto3" json:"updated_at,omitempty"` + OrgId string `protobuf:"bytes,5,opt,name=org_id,json=orgId,proto3" json:"org_id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -275,9 +276,19 @@ func (x *Resource) GetUpdatedAt() *timestamppb.Timestamp { return nil } +func (x *Resource) GetOrgId() string { + if x != nil { + return x.OrgId + } + return "" +} + +// Every request that names a resource acts in one org's copy of it. An empty +// org_id is the default org. type QueryRequest struct { state protoimpl.MessageState `protogen:"open.v1"` Kind string `protobuf:"bytes,1,opt,name=kind,proto3" json:"kind,omitempty"` + OrgId string `protobuf:"bytes,2,opt,name=org_id,json=orgId,proto3" json:"org_id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -319,6 +330,13 @@ func (x *QueryRequest) GetKind() string { return "" } +func (x *QueryRequest) GetOrgId() string { + if x != nil { + return x.OrgId + } + return "" +} + type QueryResponse struct { state protoimpl.MessageState `protogen:"open.v1"` Resource *Resource `protobuf:"bytes,1,opt,name=resource,proto3" json:"resource,omitempty"` @@ -454,7 +472,8 @@ type HistoryRequest struct { Kind string `protobuf:"bytes,1,opt,name=kind,proto3" json:"kind,omitempty"` // Limit caps the revisions returned, newest first. Zero means the server's // own default. - Limit int32 `protobuf:"varint,2,opt,name=limit,proto3" json:"limit,omitempty"` + Limit int32 `protobuf:"varint,2,opt,name=limit,proto3" json:"limit,omitempty"` + OrgId string `protobuf:"bytes,3,opt,name=org_id,json=orgId,proto3" json:"org_id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -503,6 +522,13 @@ func (x *HistoryRequest) GetLimit() int32 { return 0 } +func (x *HistoryRequest) GetOrgId() string { + if x != nil { + return x.OrgId + } + return "" +} + type HistoryResponse struct { state protoimpl.MessageState `protogen:"open.v1"` Revisions []*Revision `protobuf:"bytes,1,rep,name=revisions,proto3" json:"revisions,omitempty"` @@ -790,6 +816,7 @@ type CommandRequest struct { // from browser/request JSON. Actor string `protobuf:"bytes,6,opt,name=actor,proto3" json:"actor,omitempty"` Source string `protobuf:"bytes,7,opt,name=source,proto3" json:"source,omitempty"` + OrgId string `protobuf:"bytes,8,opt,name=org_id,json=orgId,proto3" json:"org_id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -873,6 +900,13 @@ func (x *CommandRequest) GetSource() string { return "" } +func (x *CommandRequest) GetOrgId() string { + if x != nil { + return x.OrgId + } + return "" +} + type CommandResponse struct { state protoimpl.MessageState `protogen:"open.v1"` Resource *Resource `protobuf:"bytes,1,opt,name=resource,proto3" json:"resource,omitempty"` @@ -921,6 +955,7 @@ type WatchRequest struct { state protoimpl.MessageState `protogen:"open.v1"` Kind string `protobuf:"bytes,1,opt,name=kind,proto3" json:"kind,omitempty"` AfterVersion int64 `protobuf:"varint,2,opt,name=after_version,json=afterVersion,proto3" json:"after_version,omitempty"` + OrgId string `protobuf:"bytes,3,opt,name=org_id,json=orgId,proto3" json:"org_id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -969,6 +1004,13 @@ func (x *WatchRequest) GetAfterVersion() int64 { return 0 } +func (x *WatchRequest) GetOrgId() string { + if x != nil { + return x.OrgId + } + return "" +} + type WatchResponse struct { state protoimpl.MessageState `protogen:"open.v1"` Resource *Resource `protobuf:"bytes,1,opt,name=resource,proto3" json:"resource,omitempty"` @@ -1039,16 +1081,18 @@ const file_controlplane_v1_controlplane_proto_rawDesc = "" + "\rdefaults_json\x18\b \x01(\tR\fdefaultsJson\"\x10\n" + "\x0eCatalogRequest\"T\n" + "\x0fCatalogResponse\x12A\n" + - "\tresources\x18\x01 \x03(\v2#.controlplane.v1.ResourceDescriptorR\tresources\"\x92\x01\n" + + "\tresources\x18\x01 \x03(\v2#.controlplane.v1.ResourceDescriptorR\tresources\"\xa9\x01\n" + "\bResource\x12\x12\n" + "\x04kind\x18\x01 \x01(\tR\x04kind\x12\x18\n" + "\aversion\x18\x02 \x01(\x03R\aversion\x12\x1d\n" + "\n" + "value_json\x18\x03 \x01(\fR\tvalueJson\x129\n" + "\n" + - "updated_at\x18\x04 \x01(\v2\x1a.google.protobuf.TimestampR\tupdatedAt\"\"\n" + + "updated_at\x18\x04 \x01(\v2\x1a.google.protobuf.TimestampR\tupdatedAt\x12\x15\n" + + "\x06org_id\x18\x05 \x01(\tR\x05orgId\"9\n" + "\fQueryRequest\x12\x12\n" + - "\x04kind\x18\x01 \x01(\tR\x04kind\"F\n" + + "\x04kind\x18\x01 \x01(\tR\x04kind\x12\x15\n" + + "\x06org_id\x18\x02 \x01(\tR\x05orgId\"F\n" + "\rQueryResponse\x125\n" + "\bresource\x18\x01 \x01(\v2\x19.controlplane.v1.ResourceR\bresource\"\xbc\x01\n" + "\bRevision\x12\x18\n" + @@ -1059,10 +1103,11 @@ const file_controlplane_v1_controlplane_proto_rawDesc = "" + "\x06source\x18\x04 \x01(\tR\x06source\x12\x1d\n" + "\n" + "request_id\x18\x05 \x01(\tR\trequestId\x12*\n" + - "\x02at\x18\x06 \x01(\v2\x1a.google.protobuf.TimestampR\x02at\":\n" + + "\x02at\x18\x06 \x01(\v2\x1a.google.protobuf.TimestampR\x02at\"Q\n" + "\x0eHistoryRequest\x12\x12\n" + "\x04kind\x18\x01 \x01(\tR\x04kind\x12\x14\n" + - "\x05limit\x18\x02 \x01(\x05R\x05limit\"J\n" + + "\x05limit\x18\x02 \x01(\x05R\x05limit\x12\x15\n" + + "\x06org_id\x18\x03 \x01(\tR\x05orgId\"J\n" + "\x0fHistoryResponse\x127\n" + "\trevisions\x18\x01 \x03(\v2\x19.controlplane.v1.RevisionR\trevisions\"\xc6\x02\n" + "\n" + @@ -1087,7 +1132,7 @@ const file_controlplane_v1_controlplane_proto_rawDesc = "" + "recordKeys\x12\x14\n" + "\x05limit\x18\x03 \x01(\x05R\x05limit\"F\n" + "\rAuditResponse\x125\n" + - "\aentries\x18\x01 \x03(\v2\x1b.controlplane.v1.AuditEntryR\aentries\"\xd5\x01\n" + + "\aentries\x18\x01 \x03(\v2\x1b.controlplane.v1.AuditEntryR\aentries\"\xec\x01\n" + "\x0eCommandRequest\x12\x12\n" + "\x04kind\x18\x01 \x01(\tR\x04kind\x12\x18\n" + "\acommand\x18\x02 \x01(\tR\acommand\x12\x1d\n" + @@ -1097,12 +1142,14 @@ const file_controlplane_v1_controlplane_proto_rawDesc = "" + "\n" + "request_id\x18\x05 \x01(\tR\trequestId\x12\x14\n" + "\x05actor\x18\x06 \x01(\tR\x05actor\x12\x16\n" + - "\x06source\x18\a \x01(\tR\x06source\"H\n" + + "\x06source\x18\a \x01(\tR\x06source\x12\x15\n" + + "\x06org_id\x18\b \x01(\tR\x05orgId\"H\n" + "\x0fCommandResponse\x125\n" + - "\bresource\x18\x01 \x01(\v2\x19.controlplane.v1.ResourceR\bresource\"G\n" + + "\bresource\x18\x01 \x01(\v2\x19.controlplane.v1.ResourceR\bresource\"^\n" + "\fWatchRequest\x12\x12\n" + "\x04kind\x18\x01 \x01(\tR\x04kind\x12#\n" + - "\rafter_version\x18\x02 \x01(\x03R\fafterVersion\"\\\n" + + "\rafter_version\x18\x02 \x01(\x03R\fafterVersion\x12\x15\n" + + "\x06org_id\x18\x03 \x01(\tR\x05orgId\"\\\n" + "\rWatchResponse\x125\n" + "\bresource\x18\x01 \x01(\v2\x19.controlplane.v1.ResourceR\bresource\x12\x14\n" + "\x05error\x18\x02 \x01(\tR\x05error2\xd9\x03\n" + diff --git a/internal/domain/storecontract/storecontract.go b/internal/domain/storecontract/storecontract.go index bd15d5a98..8fe884388 100644 --- a/internal/domain/storecontract/storecontract.go +++ b/internal/domain/storecontract/storecontract.go @@ -521,11 +521,12 @@ var ( ErrResourceVersionConflict = errors.New("resource version conflict") ) -// Resource is one control-plane resource document: the stored value for a -// kind, its revision, and the audit record written with the last write. +// Resource is one control-plane resource document: the stored value for an +// org's kind, its revision, and the audit record written with the last write. // Resource and ResourceWrite live here so the UI-side consumers can reference // them without linking the PostgreSQL implementation. type Resource struct { + OrgID string Kind string Value []byte Version int64 @@ -538,9 +539,23 @@ type Resource struct { } // AuditTableResources is the sys_audit table name for control-plane resources, -// keyed by resource kind. +// keyed by ResourceAuditKey. const AuditTableResources = "resources" +// DefaultOrgID is the org every resource written before orgs existed belongs +// to, and the org a request that names none acts in. +const DefaultOrgID = "default" + +// ResourceAuditKey is the sys_audit record key of one org's resource kind. The +// default org keeps the bare kind, so audit rows written before resources were +// per-org still resolve. +func ResourceAuditKey(orgID, kind string) string { + if orgID == DefaultOrgID { + return kind + } + return orgID + "/" + kind +} + // AuditEntry is one changed field of one record, as sys_audit holds it. Any // store table may write entries; Table and RecordKey locate the record. // OldValue and NewValue are JSON; an absent side is empty. @@ -562,6 +577,7 @@ type AuditEntry struct { // the optimistic-concurrency guard: a write whose expectation does not match // the stored revision is refused with ErrResourceVersionConflict. type ResourceWrite struct { + OrgID string Kind string Value []byte Actor string diff --git a/internal/infrastructure/postgres/migrations/0021_resources_per_org.sql b/internal/infrastructure/postgres/migrations/0021_resources_per_org.sql new file mode 100644 index 000000000..7ac427bf1 --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0021_resources_per_org.sql @@ -0,0 +1,24 @@ +-- +goose Up + +-- A control-plane resource is one document per (org, kind). Every existing row +-- already carries org_id 'default' from 0013, so the wider key holds wherever +-- the kind-only one did. +ALTER TABLE resources DROP CONSTRAINT resources_pkey; +ALTER TABLE resources ADD PRIMARY KEY (org_id, kind); + +DROP INDEX idx_resource_history_kind_version; +DROP INDEX idx_resource_history_kind_request; +CREATE INDEX idx_resource_history_org_kind_version ON resource_history (org_id, kind, version); +-- The request-ID idempotency key is per org and kind: a resource write replays +-- only the same org's prior write of the same kind (resourceByRequest). +CREATE UNIQUE INDEX idx_resource_history_org_kind_request ON resource_history (org_id, kind, request_id); + +-- +goose Down + +DROP INDEX idx_resource_history_org_kind_request; +DROP INDEX idx_resource_history_org_kind_version; +CREATE UNIQUE INDEX idx_resource_history_kind_request ON resource_history (kind, request_id); +CREATE INDEX idx_resource_history_kind_version ON resource_history (kind, version); + +ALTER TABLE resources DROP CONSTRAINT resources_pkey; +ALTER TABLE resources ADD PRIMARY KEY (kind); diff --git a/internal/infrastructure/postgres/postgresdb/audit.sql.go b/internal/infrastructure/postgres/postgresdb/audit.sql.go index 0e3867ad6..becd1d945 100644 --- a/internal/infrastructure/postgres/postgresdb/audit.sql.go +++ b/internal/infrastructure/postgres/postgresdb/audit.sql.go @@ -62,19 +62,21 @@ SELECT $1::timestamptz, 'resources', $2::text, d.field, d.old_value, d.new_value $3::bigint, $4::text, $5::text, $6::text FROM audit_json_diff( (SELECT convert_from(h.value, 'UTF8')::jsonb FROM resource_history h - WHERE h.kind = $2::text AND h.version = $7::bigint), - convert_from($8::bytea, 'UTF8')::jsonb + WHERE h.org_id = $7::text AND h.kind = $8::text AND h.version = $9::bigint), + convert_from($10::bytea, 'UTF8')::jsonb ) d ORDER BY d.field ` type InsertResourceAuditParams struct { At time.Time - Kind string + RecordKey string Version int64 Actor string Source string RequestID string + OrgID string + Kind string PreviousVersion int64 Value []byte } @@ -84,11 +86,13 @@ type InsertResourceAuditParams struct { func (q *Queries) InsertResourceAudit(ctx context.Context, arg InsertResourceAuditParams) error { _, err := q.db.Exec(ctx, insertResourceAudit, arg.At, - arg.Kind, + arg.RecordKey, arg.Version, arg.Actor, arg.Source, arg.RequestID, + arg.OrgID, + arg.Kind, arg.PreviousVersion, arg.Value, ) diff --git a/internal/infrastructure/postgres/postgresdb/resources.sql.go b/internal/infrastructure/postgres/postgresdb/resources.sql.go index 5a298458f..d1c8c64ce 100644 --- a/internal/infrastructure/postgres/postgresdb/resources.sql.go +++ b/internal/infrastructure/postgres/postgresdb/resources.sql.go @@ -11,19 +11,25 @@ import ( ) const insertResource = `-- name: InsertResource :one -INSERT INTO resources (kind, value, version, updated_at) -VALUES ($1, $2, 1, $3) +INSERT INTO resources (org_id, kind, value, version, updated_at) +VALUES ($1, $2, $3, 1, $4) RETURNING kind, value, version, updated_at, org_id ` type InsertResourceParams struct { + OrgID string Kind string Value []byte UpdatedAt time.Time } func (q *Queries) InsertResource(ctx context.Context, arg InsertResourceParams) (Resource, error) { - row := q.db.QueryRow(ctx, insertResource, arg.Kind, arg.Value, arg.UpdatedAt) + row := q.db.QueryRow(ctx, insertResource, + arg.OrgID, + arg.Kind, + arg.Value, + arg.UpdatedAt, + ) var i Resource err := row.Scan( &i.Kind, @@ -37,15 +43,16 @@ func (q *Queries) InsertResource(ctx context.Context, arg InsertResourceParams) const insertResourceHistory = `-- name: InsertResourceHistory :one INSERT INTO resource_history ( - kind, value, version, actor, source, request_id, expected_version, + org_id, kind, value, version, actor, source, request_id, expected_version, current_version, at ) -VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) -RETURNING kind, value, version, actor, source, request_id, expected_version, +VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) +RETURNING org_id, kind, value, version, actor, source, request_id, expected_version, current_version, at ` type InsertResourceHistoryParams struct { + OrgID string Kind string Value []byte Version int64 @@ -58,6 +65,7 @@ type InsertResourceHistoryParams struct { } type InsertResourceHistoryRow struct { + OrgID string Kind string Value []byte Version int64 @@ -71,6 +79,7 @@ type InsertResourceHistoryRow struct { func (q *Queries) InsertResourceHistory(ctx context.Context, arg InsertResourceHistoryParams) (InsertResourceHistoryRow, error) { row := q.db.QueryRow(ctx, insertResourceHistory, + arg.OrgID, arg.Kind, arg.Value, arg.Version, @@ -83,6 +92,7 @@ func (q *Queries) InsertResourceHistory(ctx context.Context, arg InsertResourceH ) var i InsertResourceHistoryRow err := row.Scan( + &i.OrgID, &i.Kind, &i.Value, &i.Version, @@ -97,24 +107,31 @@ func (q *Queries) InsertResourceHistory(ctx context.Context, arg InsertResourceH } const lockResourceWrite = `-- name: LockResourceWrite :exec -SELECT pg_advisory_xact_lock(hashtextextended($1, 0)) +SELECT pg_advisory_xact_lock(hashtextextended($1::text || '/' || $2::text, 0)) ` +type LockResourceWriteParams struct { + OrgID string + Kind string +} + // This lock is acquired in its own statement before a resource read. PostgreSQL // takes a fresh Read Committed snapshot for the subsequent read, so concurrent // optimistic writers see the committed revision rather than both writing v1. -func (q *Queries) LockResourceWrite(ctx context.Context, hashtextextended string) error { - _, err := q.db.Exec(ctx, lockResourceWrite, hashtextextended) +// The lock is per (org, kind): two orgs writing the same kind do not contend. +func (q *Queries) LockResourceWrite(ctx context.Context, arg LockResourceWriteParams) error { + _, err := q.db.Exec(ctx, lockResourceWrite, arg.OrgID, arg.Kind) return err } const reinsertResource = `-- name: ReinsertResource :one -INSERT INTO resources (kind, value, version, updated_at) -VALUES ($1, $2, $3, $4) +INSERT INTO resources (org_id, kind, value, version, updated_at) +VALUES ($1, $2, $3, $4, $5) RETURNING kind, value, version, updated_at, org_id ` type ReinsertResourceParams struct { + OrgID string Kind string Value []byte Version int64 @@ -124,11 +141,12 @@ type ReinsertResourceParams struct { // Re-creates the current row for a revision resource_history already records. // The ledger outlives the row it describes, so a resource an operator removed // still has its revisions; putting one back needs the row re-created without a -// second ledger entry, which idx_resource_history_kind_request would refuse. +// second ledger entry, which idx_resource_history_org_kind_request would refuse. // The revision is the ledger's, not a fresh one: this is the row that write // produced, restored. func (q *Queries) ReinsertResource(ctx context.Context, arg ReinsertResourceParams) (Resource, error) { row := q.db.QueryRow(ctx, reinsertResource, + arg.OrgID, arg.Kind, arg.Value, arg.Version, @@ -146,11 +164,17 @@ func (q *Queries) ReinsertResource(ctx context.Context, arg ReinsertResourcePara } const resourceByKind = `-- name: ResourceByKind :one -SELECT kind, value, version, updated_at, org_id FROM resources WHERE kind = $1 +SELECT kind, value, version, updated_at, org_id FROM resources +WHERE org_id = $1 AND kind = $2 ` -func (q *Queries) ResourceByKind(ctx context.Context, kind string) (Resource, error) { - row := q.db.QueryRow(ctx, resourceByKind, kind) +type ResourceByKindParams struct { + OrgID string + Kind string +} + +func (q *Queries) ResourceByKind(ctx context.Context, arg ResourceByKindParams) (Resource, error) { + row := q.db.QueryRow(ctx, resourceByKind, arg.OrgID, arg.Kind) var i Resource err := row.Scan( &i.Kind, @@ -163,18 +187,20 @@ func (q *Queries) ResourceByKind(ctx context.Context, kind string) (Resource, er } const resourceByRequest = `-- name: ResourceByRequest :one -SELECT kind, value, version, actor, source, request_id, expected_version, +SELECT org_id, kind, value, version, actor, source, request_id, expected_version, current_version, at FROM resource_history -WHERE kind = $1 AND request_id = $2 +WHERE org_id = $1 AND kind = $2 AND request_id = $3 ` type ResourceByRequestParams struct { + OrgID string Kind string RequestID string } type ResourceByRequestRow struct { + OrgID string Kind string Value []byte Version int64 @@ -187,9 +213,10 @@ type ResourceByRequestRow struct { } func (q *Queries) ResourceByRequest(ctx context.Context, arg ResourceByRequestParams) (ResourceByRequestRow, error) { - row := q.db.QueryRow(ctx, resourceByRequest, arg.Kind, arg.RequestID) + row := q.db.QueryRow(ctx, resourceByRequest, arg.OrgID, arg.Kind, arg.RequestID) var i ResourceByRequestRow err := row.Scan( + &i.OrgID, &i.Kind, &i.Value, &i.Version, @@ -204,20 +231,22 @@ func (q *Queries) ResourceByRequest(ctx context.Context, arg ResourceByRequestPa } const resourceHistory = `-- name: ResourceHistory :many -SELECT kind, value, version, actor, source, request_id, expected_version, +SELECT org_id, kind, value, version, actor, source, request_id, expected_version, current_version, at FROM resource_history -WHERE kind = $1 +WHERE org_id = $1 AND kind = $2 ORDER BY version DESC -LIMIT NULLIF($2::bigint, 0) +LIMIT NULLIF($3::bigint, 0) ` type ResourceHistoryParams struct { + OrgID string Kind string EntryLimit int64 } type ResourceHistoryRow struct { + OrgID string Kind string Value []byte Version int64 @@ -230,7 +259,7 @@ type ResourceHistoryRow struct { } func (q *Queries) ResourceHistory(ctx context.Context, arg ResourceHistoryParams) ([]ResourceHistoryRow, error) { - rows, err := q.db.Query(ctx, resourceHistory, arg.Kind, arg.EntryLimit) + rows, err := q.db.Query(ctx, resourceHistory, arg.OrgID, arg.Kind, arg.EntryLimit) if err != nil { return nil, err } @@ -239,6 +268,7 @@ func (q *Queries) ResourceHistory(ctx context.Context, arg ResourceHistoryParams for rows.Next() { var i ResourceHistoryRow if err := rows.Scan( + &i.OrgID, &i.Kind, &i.Value, &i.Version, @@ -260,11 +290,16 @@ func (q *Queries) ResourceHistory(ctx context.Context, arg ResourceHistoryParams } const resourceVersion = `-- name: ResourceVersion :one -SELECT version FROM resources WHERE kind = $1 +SELECT version FROM resources WHERE org_id = $1 AND kind = $2 ` -func (q *Queries) ResourceVersion(ctx context.Context, kind string) (int64, error) { - row := q.db.QueryRow(ctx, resourceVersion, kind) +type ResourceVersionParams struct { + OrgID string + Kind string +} + +func (q *Queries) ResourceVersion(ctx context.Context, arg ResourceVersionParams) (int64, error) { + row := q.db.QueryRow(ctx, resourceVersion, arg.OrgID, arg.Kind) var version int64 err := row.Scan(&version) return version, err @@ -272,12 +307,13 @@ func (q *Queries) ResourceVersion(ctx context.Context, kind string) (int64, erro const updateResource = `-- name: UpdateResource :one UPDATE resources -SET value = $2, version = version + 1, updated_at = $3 -WHERE kind = $1 AND version = $4 +SET value = $3, version = version + 1, updated_at = $4 +WHERE org_id = $1 AND kind = $2 AND version = $5 RETURNING kind, value, version, updated_at, org_id ` type UpdateResourceParams struct { + OrgID string Kind string Value []byte UpdatedAt time.Time @@ -286,6 +322,7 @@ type UpdateResourceParams struct { func (q *Queries) UpdateResource(ctx context.Context, arg UpdateResourceParams) (Resource, error) { row := q.db.QueryRow(ctx, updateResource, + arg.OrgID, arg.Kind, arg.Value, arg.UpdatedAt, diff --git a/internal/infrastructure/postgres/queries/audit.sql b/internal/infrastructure/postgres/queries/audit.sql index 62065d7f5..f87cce609 100644 --- a/internal/infrastructure/postgres/queries/audit.sql +++ b/internal/infrastructure/postgres/queries/audit.sql @@ -2,11 +2,11 @@ -- Records the fields one resource write changed, against the version it -- replaced. Runs in the write's own transaction. INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id) -SELECT sqlc.arg(at)::timestamptz, 'resources', sqlc.arg(kind)::text, d.field, d.old_value, d.new_value, +SELECT sqlc.arg(at)::timestamptz, 'resources', sqlc.arg(record_key)::text, d.field, d.old_value, d.new_value, sqlc.arg(version)::bigint, sqlc.arg(actor)::text, sqlc.arg(source)::text, sqlc.arg(request_id)::text FROM audit_json_diff( (SELECT convert_from(h.value, 'UTF8')::jsonb FROM resource_history h - WHERE h.kind = sqlc.arg(kind)::text AND h.version = sqlc.arg(previous_version)::bigint), + WHERE h.org_id = sqlc.arg(org_id)::text AND h.kind = sqlc.arg(kind)::text AND h.version = sqlc.arg(previous_version)::bigint), convert_from(sqlc.arg(value)::bytea, 'UTF8')::jsonb ) d ORDER BY d.field; diff --git a/internal/infrastructure/postgres/queries/resources.sql b/internal/infrastructure/postgres/queries/resources.sql index e094cb90c..6d9117a4c 100644 --- a/internal/infrastructure/postgres/queries/resources.sql +++ b/internal/infrastructure/postgres/queries/resources.sql @@ -2,55 +2,57 @@ -- This lock is acquired in its own statement before a resource read. PostgreSQL -- takes a fresh Read Committed snapshot for the subsequent read, so concurrent -- optimistic writers see the committed revision rather than both writing v1. -SELECT pg_advisory_xact_lock(hashtextextended($1, 0)); +-- The lock is per (org, kind): two orgs writing the same kind do not contend. +SELECT pg_advisory_xact_lock(hashtextextended(sqlc.arg(org_id)::text || '/' || sqlc.arg(kind)::text, 0)); -- name: ResourceByKind :one -SELECT kind, value, version, updated_at, org_id FROM resources WHERE kind = $1; +SELECT kind, value, version, updated_at, org_id FROM resources +WHERE org_id = $1 AND kind = $2; -- name: ResourceByRequest :one -SELECT kind, value, version, actor, source, request_id, expected_version, +SELECT org_id, kind, value, version, actor, source, request_id, expected_version, current_version, at FROM resource_history -WHERE kind = $1 AND request_id = $2; +WHERE org_id = $1 AND kind = $2 AND request_id = $3; -- name: ReinsertResource :one -- Re-creates the current row for a revision resource_history already records. -- The ledger outlives the row it describes, so a resource an operator removed -- still has its revisions; putting one back needs the row re-created without a --- second ledger entry, which idx_resource_history_kind_request would refuse. +-- second ledger entry, which idx_resource_history_org_kind_request would refuse. -- The revision is the ledger's, not a fresh one: this is the row that write -- produced, restored. -INSERT INTO resources (kind, value, version, updated_at) -VALUES ($1, $2, $3, $4) +INSERT INTO resources (org_id, kind, value, version, updated_at) +VALUES ($1, $2, $3, $4, $5) RETURNING kind, value, version, updated_at, org_id; -- name: ResourceHistory :many -SELECT kind, value, version, actor, source, request_id, expected_version, +SELECT org_id, kind, value, version, actor, source, request_id, expected_version, current_version, at FROM resource_history -WHERE kind = $1 +WHERE org_id = $1 AND kind = $2 ORDER BY version DESC LIMIT NULLIF(sqlc.arg(entry_limit)::bigint, 0); -- name: ResourceVersion :one -SELECT version FROM resources WHERE kind = $1; +SELECT version FROM resources WHERE org_id = $1 AND kind = $2; -- name: InsertResource :one -INSERT INTO resources (kind, value, version, updated_at) -VALUES ($1, $2, 1, $3) +INSERT INTO resources (org_id, kind, value, version, updated_at) +VALUES ($1, $2, $3, 1, $4) RETURNING kind, value, version, updated_at, org_id; -- name: UpdateResource :one UPDATE resources -SET value = $2, version = version + 1, updated_at = $3 -WHERE kind = $1 AND version = $4 +SET value = $3, version = version + 1, updated_at = $4 +WHERE org_id = $1 AND kind = $2 AND version = $5 RETURNING kind, value, version, updated_at, org_id; -- name: InsertResourceHistory :one INSERT INTO resource_history ( - kind, value, version, actor, source, request_id, expected_version, + org_id, kind, value, version, actor, source, request_id, expected_version, current_version, at ) -VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) -RETURNING kind, value, version, actor, source, request_id, expected_version, +VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) +RETURNING org_id, kind, value, version, actor, source, request_id, expected_version, current_version, at; diff --git a/internal/infrastructure/postgres/resources.go b/internal/infrastructure/postgres/resources.go index c7bed3c19..ee15f4504 100644 --- a/internal/infrastructure/postgres/resources.go +++ b/internal/infrastructure/postgres/resources.go @@ -24,8 +24,8 @@ func NewResources(pool *pgxpool.Pool) *Resources { return &Resources{pool: pool} } -func (s *Resources) Resource(ctx context.Context, kind string) (storecontract.Resource, error) { - resource, err := postgresdb.New(s.pool).ResourceByKind(ctx, kind) +func (s *Resources) Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) { + resource, err := postgresdb.New(s.pool).ResourceByKind(ctx, postgresdb.ResourceByKindParams{OrgID: orgID, Kind: kind}) if errors.Is(err, pgx.ErrNoRows) { return storecontract.Resource{}, storecontract.ErrResourceNotFound } @@ -35,12 +35,12 @@ func (s *Resources) Resource(ctx context.Context, kind string) (storecontract.Re return resourceFromCurrent(resource), nil } -func (s *Resources) ResourceHistory(ctx context.Context, kind string, limit int) ([]storecontract.Resource, error) { +func (s *Resources) ResourceHistory(ctx context.Context, orgID, kind string, limit int) ([]storecontract.Resource, error) { if limit <= 0 { limit = 0 } history, err := postgresdb.New(s.pool).ResourceHistory(ctx, postgresdb.ResourceHistoryParams{ - Kind: kind, EntryLimit: int64(limit), + OrgID: orgID, Kind: kind, EntryLimit: int64(limit), }) if err != nil { return nil, err @@ -48,7 +48,7 @@ func (s *Resources) ResourceHistory(ctx context.Context, kind string, limit int) resources := make([]storecontract.Resource, 0, len(history)) for _, entry := range history { resources = append(resources, resourceFromAudit( - entry.Kind, entry.Value, entry.Version, entry.Actor, entry.Source, + entry.OrgID, entry.Kind, entry.Value, entry.Version, entry.Actor, entry.Source, entry.RequestID, entry.ExpectedVersion, entry.CurrentVersion, entry.At, )) } @@ -80,7 +80,7 @@ func (s *Resources) Audit(ctx context.Context, table string, keys []string, limi // PutResource applies one control-plane resource write. // -// It is idempotent per request ID: a write whose (kind, request_id) is already +// It is idempotent per request ID: a write whose (org_id, kind, request_id) is already // in resource_history returns that revision instead of writing a second one, so a // retry that reached the store twice is still one revision. The ledger is what // carries the key -- resource_history outlives the row it describes -- so a @@ -90,17 +90,20 @@ func (s *Resources) Audit(ctx context.Context, table string, keys []string, limi // caller cannot tell the difference, and a kind can stay gone while every // process believes it holds a value. func (s *Resources) PutResource(ctx context.Context, write storecontract.ResourceWrite) (_ storecontract.Resource, retErr error) { + if write.OrgID == "" { + return storecontract.Resource{}, errors.New("resource write names no org") + } tx, err := s.pool.Begin(ctx) if err != nil { return storecontract.Resource{}, err } defer func() { _ = tx.Rollback(ctx) }() queries := postgresdb.New(tx) - if err := queries.LockResourceWrite(ctx, write.Kind); err != nil { + if err := queries.LockResourceWrite(ctx, postgresdb.LockResourceWriteParams{OrgID: write.OrgID, Kind: write.Kind}); err != nil { return storecontract.Resource{}, err } if existing, err := queries.ResourceByRequest(ctx, postgresdb.ResourceByRequestParams{ - Kind: write.Kind, RequestID: write.RequestID, + OrgID: write.OrgID, Kind: write.Kind, RequestID: write.RequestID, }); err == nil { resource, err := s.replayedResource(ctx, queries, tx, existing) if err != nil { @@ -111,7 +114,7 @@ func (s *Resources) PutResource(ctx context.Context, write storecontract.Resourc return storecontract.Resource{}, err } - current, err := queries.ResourceVersion(ctx, write.Kind) + current, err := queries.ResourceVersion(ctx, postgresdb.ResourceVersionParams{OrgID: write.OrgID, Kind: write.Kind}) if errors.Is(err, pgx.ErrNoRows) { current = 0 } else if err != nil { @@ -120,48 +123,62 @@ func (s *Resources) PutResource(ctx context.Context, write storecontract.Resourc if current != write.ExpectedVersion { return storecontract.Resource{}, fmt.Errorf("%w: expected %d, current %d", storecontract.ErrResourceVersionConflict, write.ExpectedVersion, current) } - if write.At.IsZero() { - write.At = time.Now().UTC() - } else { - write.At = write.At.UTC() - } - - var resource postgresdb.Resource - if current == 0 { - resource, err = queries.InsertResource(ctx, postgresdb.InsertResourceParams{ - Kind: write.Kind, Value: write.Value, UpdatedAt: write.At, - }) - } else { - resource, err = queries.UpdateResource(ctx, postgresdb.UpdateResourceParams{ - Kind: write.Kind, Value: write.Value, UpdatedAt: write.At, Version: current, - }) - } + history, err := appendResourceRevision(ctx, queries, write, current) if err != nil { return storecontract.Resource{}, err } - history, err := queries.InsertResourceHistory(ctx, postgresdb.InsertResourceHistoryParams{ - Kind: write.Kind, Value: write.Value, Version: resource.Version, Actor: write.Actor, - Source: write.Source, RequestID: write.RequestID, ExpectedVersion: write.ExpectedVersion, - CurrentVersion: current, At: write.At, - }) - if err != nil { - return storecontract.Resource{}, err - } - if err := queries.InsertResourceAudit(ctx, postgresdb.InsertResourceAuditParams{ - At: write.At, Kind: write.Kind, Version: resource.Version, Actor: write.Actor, - Source: write.Source, RequestID: write.RequestID, PreviousVersion: current, Value: write.Value, - }); err != nil { - return storecontract.Resource{}, err - } if err := tx.Commit(ctx); err != nil { return storecontract.Resource{}, err } return resourceFromAudit( - history.Kind, history.Value, history.Version, history.Actor, history.Source, + history.OrgID, history.Kind, history.Value, history.Version, history.Actor, history.Source, history.RequestID, history.ExpectedVersion, history.CurrentVersion, history.At, ), nil } +// appendResourceRevision writes the resource row, its ledger entry and its audit +// trail for one accepted write, returning the ledger entry. current is the +// version being replaced, zero for a first write. +func appendResourceRevision(ctx context.Context, queries *postgresdb.Queries, write storecontract.ResourceWrite, current int64) (postgresdb.InsertResourceHistoryRow, error) { + if write.At.IsZero() { + write.At = time.Now().UTC() + } else { + write.At = write.At.UTC() + } + + var ( + resource postgresdb.Resource + err error + ) + if current == 0 { + resource, err = queries.InsertResource(ctx, postgresdb.InsertResourceParams{ + OrgID: write.OrgID, Kind: write.Kind, Value: write.Value, UpdatedAt: write.At, + }) + } else { + resource, err = queries.UpdateResource(ctx, postgresdb.UpdateResourceParams{ + OrgID: write.OrgID, Kind: write.Kind, Value: write.Value, UpdatedAt: write.At, Version: current, + }) + } + if err != nil { + return postgresdb.InsertResourceHistoryRow{}, err + } + history, err := queries.InsertResourceHistory(ctx, postgresdb.InsertResourceHistoryParams{ + OrgID: write.OrgID, Kind: write.Kind, Value: write.Value, Version: resource.Version, Actor: write.Actor, + Source: write.Source, RequestID: write.RequestID, ExpectedVersion: write.ExpectedVersion, + CurrentVersion: current, At: write.At, + }) + if err != nil { + return postgresdb.InsertResourceHistoryRow{}, err + } + if err := queries.InsertResourceAudit(ctx, postgresdb.InsertResourceAuditParams{ + At: write.At, OrgID: write.OrgID, RecordKey: storecontract.ResourceAuditKey(write.OrgID, write.Kind), Kind: write.Kind, Version: resource.Version, Actor: write.Actor, + Source: write.Source, RequestID: write.RequestID, PreviousVersion: current, Value: write.Value, + }); err != nil { + return postgresdb.InsertResourceHistoryRow{}, err + } + return history, nil +} + // replayedResource answers a write whose request ID resource_history already // holds. The ledger's revision is the answer and a replay writes nothing, with // one repair: when the resource that revision describes is no longer the current @@ -170,9 +187,9 @@ func (s *Resources) PutResource(ctx context.Context, write storecontract.Resourc // revision it does not hold, so the caller reads a version for a kind that is // absent and the kind stays absent. func (s *Resources) replayedResource(ctx context.Context, queries *postgresdb.Queries, tx pgx.Tx, existing postgresdb.ResourceByRequestRow) (storecontract.Resource, error) { - if _, err := queries.ResourceByKind(ctx, existing.Kind); errors.Is(err, pgx.ErrNoRows) { + if _, err := queries.ResourceByKind(ctx, postgresdb.ResourceByKindParams{OrgID: existing.OrgID, Kind: existing.Kind}); errors.Is(err, pgx.ErrNoRows) { if _, err := queries.ReinsertResource(ctx, postgresdb.ReinsertResourceParams{ - Kind: existing.Kind, Value: existing.Value, Version: existing.Version, UpdatedAt: existing.At, + OrgID: existing.OrgID, Kind: existing.Kind, Value: existing.Value, Version: existing.Version, UpdatedAt: existing.At, }); err != nil { return storecontract.Resource{}, err } @@ -183,19 +200,19 @@ func (s *Resources) replayedResource(ctx context.Context, queries *postgresdb.Qu return storecontract.Resource{}, err } return resourceFromAudit( - existing.Kind, existing.Value, existing.Version, existing.Actor, existing.Source, + existing.OrgID, existing.Kind, existing.Value, existing.Version, existing.Actor, existing.Source, existing.RequestID, existing.ExpectedVersion, existing.CurrentVersion, existing.At, ), nil } func resourceFromCurrent(resource postgresdb.Resource) storecontract.Resource { return storecontract.Resource{ - Kind: resource.Kind, Value: resource.Value, Version: resource.Version, At: resource.UpdatedAt, + OrgID: resource.OrgID, Kind: resource.Kind, Value: resource.Value, Version: resource.Version, At: resource.UpdatedAt, } } func resourceFromAudit( - kind string, + orgID, kind string, value []byte, version int64, actor, source, requestID string, @@ -203,7 +220,7 @@ func resourceFromAudit( at time.Time, ) storecontract.Resource { return storecontract.Resource{ - Kind: kind, Value: value, Version: version, Actor: actor, Source: source, + OrgID: orgID, Kind: kind, Value: value, Version: version, Actor: actor, Source: source, RequestID: requestID, ExpectedVersion: expectedVersion, CurrentVersion: currentVersion, At: at, } } diff --git a/internal/infrastructure/postgres/statestore_test.go b/internal/infrastructure/postgres/statestore_test.go index 7b463cac6..77c1c86d7 100644 --- a/internal/infrastructure/postgres/statestore_test.go +++ b/internal/infrastructure/postgres/statestore_test.go @@ -192,7 +192,8 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) { resources := resourcesFor(t) firstAt := time.Date(2026, 9, 24, 1, 0, 0, 0, time.UTC) first, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "archie-ui", + OrgID: storecontract.DefaultOrgID, + Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "archie-ui", RequestID: "r1", ExpectedVersion: 0, At: firstAt, }) if err != nil { @@ -203,7 +204,8 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) { } replay, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "settings", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry", + OrgID: storecontract.DefaultOrgID, + Kind: "settings", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry", RequestID: "r1", ExpectedVersion: 0, At: firstAt.Add(time.Minute), }) if err != nil { @@ -214,14 +216,16 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) { } if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "archie-ui", + OrgID: storecontract.DefaultOrgID, + Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "archie-ui", RequestID: "r2", ExpectedVersion: 0, At: firstAt.Add(2 * time.Minute), }); !errors.Is(err, storecontract.ErrResourceVersionConflict) { t.Fatalf("stale PutResource = %v, want ErrResourceVersionConflict", err) } second, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "messaging", + OrgID: storecontract.DefaultOrgID, + Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "messaging", RequestID: "r2", ExpectedVersion: 1, At: firstAt.Add(3 * time.Minute), }) if err != nil { @@ -231,7 +235,8 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) { t.Fatalf("second write = %+v, want version 2 from version 1", second) } lateReplay, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "settings", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry", + OrgID: storecontract.DefaultOrgID, + Kind: "settings", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry", RequestID: "r1", ExpectedVersion: 0, At: firstAt.Add(4 * time.Minute), }) if err != nil { @@ -240,7 +245,7 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) { if lateReplay.Version != first.Version || string(lateReplay.Value) != string(first.Value) { t.Fatalf("late replay = %+v, want original %+v", lateReplay, first) } - live, err := resources.Resource(t.Context(), "settings") + live, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, "settings") if err != nil { t.Fatalf("Resource: %v", err) } @@ -248,21 +253,21 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) { t.Fatalf("live resource after replay = %+v, want second write %+v", live, second) } - history, err := resources.ResourceHistory(t.Context(), "settings", 1) + history, err := resources.ResourceHistory(t.Context(), storecontract.DefaultOrgID, "settings", 1) if err != nil { t.Fatalf("ResourceHistory: %v", err) } if len(history) != 1 || history[0].Version != 2 || history[0].Source != "messaging" { t.Fatalf("limited history = %+v, want newest revision only", history) } - history, err = resources.ResourceHistory(t.Context(), "settings", 0) + history, err = resources.ResourceHistory(t.Context(), storecontract.DefaultOrgID, "settings", 0) if err != nil { t.Fatalf("unlimited ResourceHistory: %v", err) } if len(history) != 2 { t.Fatalf("history after late replay = %+v, want two revisions", history) } - if _, err := resources.Resource(t.Context(), "missing"); !errors.Is(err, storecontract.ErrResourceNotFound) { + if _, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, "missing"); !errors.Is(err, storecontract.ErrResourceNotFound) { t.Fatalf("missing Resource = %v, want ErrResourceNotFound", err) } } @@ -278,7 +283,8 @@ func TestResourceReplayRecreatesARemovedResource(t *testing.T) { resources := NewResources(pool) at := time.Date(2026, 9, 27, 9, 0, 0, 0, time.UTC) write := storecontract.ResourceWrite{ - Kind: "workflow-definitions", Value: []byte(`{"definitions":[]}`), Actor: "system:migration", + OrgID: storecontract.DefaultOrgID, + Kind: "workflow-definitions", Value: []byte(`{"definitions":[]}`), Actor: "system:migration", Source: "legacy-config", RequestID: "import:workflow-definitions", ExpectedVersion: 0, At: at, } seeded, err := resources.PutResource(t.Context(), write) @@ -289,7 +295,7 @@ func TestResourceReplayRecreatesARemovedResource(t *testing.T) { if _, err := pool.Exec(t.Context(), `DELETE FROM resources WHERE kind = $1`, write.Kind); err != nil { t.Fatalf("remove the resource: %v", err) } - if _, err := resources.Resource(t.Context(), write.Kind); !errors.Is(err, storecontract.ErrResourceNotFound) { + if _, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, write.Kind); !errors.Is(err, storecontract.ErrResourceNotFound) { t.Fatalf("resource after the removal = %v, want ErrResourceNotFound", err) } @@ -300,7 +306,7 @@ func TestResourceReplayRecreatesARemovedResource(t *testing.T) { if replayed.Version != seeded.Version || string(replayed.Value) != string(seeded.Value) { t.Fatalf("replay = %+v, want the revision the ledger records, %+v", replayed, seeded) } - live, err := resources.Resource(t.Context(), write.Kind) + live, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, write.Kind) if err != nil { t.Fatalf("the replay did not re-create the resource: %v", err) } @@ -309,7 +315,7 @@ func TestResourceReplayRecreatesARemovedResource(t *testing.T) { } // The repair re-creates the row the write produced; it does not append a // second ledger entry, which idx_resource_history_kind_request would refuse. - history, err := resources.ResourceHistory(t.Context(), write.Kind, 0) + history, err := resources.ResourceHistory(t.Context(), storecontract.DefaultOrgID, write.Kind, 0) if err != nil { t.Fatalf("ResourceHistory: %v", err) } @@ -320,7 +326,8 @@ func TestResourceReplayRecreatesARemovedResource(t *testing.T) { // A resource the store still holds is answered from the ledger, unchanged: // the idempotent replay the index exists for. again, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: write.Kind, Value: []byte(`{"definitions":[{"id":"different"}]}`), Actor: "other", + OrgID: storecontract.DefaultOrgID, + Kind: write.Kind, Value: []byte(`{"definitions":[{"id":"different"}]}`), Actor: "other", Source: "retry", RequestID: write.RequestID, ExpectedVersion: 0, At: at.Add(time.Minute), }) if err != nil { @@ -340,14 +347,16 @@ func TestResourceRequestIDIsIdempotentPerKind(t *testing.T) { resources := resourcesFor(t) at := time.Date(2026, 9, 26, 12, 0, 0, 0, time.UTC) first, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "provider-settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "import", + OrgID: storecontract.DefaultOrgID, + Kind: "provider-settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "import", RequestID: "shared", ExpectedVersion: 0, At: at, }) if err != nil { t.Fatalf("first PutResource: %v", err) } second, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "scheduling-policy", Value: []byte(`{"v":2}`), Actor: "operator", Source: "import", + OrgID: storecontract.DefaultOrgID, + Kind: "scheduling-policy", Value: []byte(`{"v":2}`), Actor: "operator", Source: "import", RequestID: "shared", ExpectedVersion: 0, At: at, }) if err != nil { @@ -358,7 +367,8 @@ func TestResourceRequestIDIsIdempotentPerKind(t *testing.T) { } replay, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "scheduling-policy", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry", + OrgID: storecontract.DefaultOrgID, + Kind: "scheduling-policy", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry", RequestID: "shared", ExpectedVersion: 0, At: at.Add(time.Minute), }) if err != nil { @@ -368,7 +378,7 @@ func TestResourceRequestIDIsIdempotentPerKind(t *testing.T) { t.Fatalf("replay = %+v, want scheduling-policy's original %+v", replay, second) } - live, err := resources.Resource(t.Context(), "scheduling-policy") + live, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, "scheduling-policy") if err != nil { t.Fatalf("Resource: %v", err) } @@ -389,7 +399,8 @@ func TestConcurrentResourceWritesAcceptOnlyOneExpectedVersion(t *testing.T) { go func() { <-start _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "test", + OrgID: storecontract.DefaultOrgID, + Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "test", RequestID: requestID, ExpectedVersion: 0, }) results <- result{err: err} @@ -413,7 +424,7 @@ func TestConcurrentResourceWritesAcceptOnlyOneExpectedVersion(t *testing.T) { t.Fatalf("concurrent writes = %d succeeded, %d conflicted; want 1 each", succeeded, conflicted) } - history, err := resources.ResourceHistory(t.Context(), "settings", 0) + history, err := resources.ResourceHistory(t.Context(), storecontract.DefaultOrgID, "settings", 0) if err != nil { t.Fatalf("ResourceHistory: %v", err) } @@ -429,7 +440,8 @@ func TestResourceWritesRecordFieldLevelAudit(t *testing.T) { put := func(value string, expected int64, request string) { t.Helper() if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "settings", Value: []byte(value), Actor: "sam", Source: "archie-ui", + OrgID: storecontract.DefaultOrgID, + Kind: "settings", Value: []byte(value), Actor: "sam", Source: "archie-ui", RequestID: request, ExpectedVersion: expected, }); err != nil { t.Fatalf("PutResource(%s): %v", value, err) @@ -455,3 +467,66 @@ func TestResourceWritesRecordFieldLevelAudit(t *testing.T) { t.Fatalf("audit =\n%s\nwant\n%s", strings.Join(got, "\n"), strings.Join(want, "\n")) } } + +// A kind is one document per org: two orgs write the same kind under the same +// request ID and each keeps its own value, revision, history and audit trail. +func TestResourcesAreIndependentPerOrg(t *testing.T) { + resources := resourcesFor(t) + at := time.Date(2026, 9, 30, 12, 0, 0, 0, time.UTC) + put := func(orgID, value string, expected int64) storecontract.Resource { + t.Helper() + got, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ + OrgID: orgID, Kind: "workflow-definitions", Value: []byte(value), Actor: "operator", + Source: "test", RequestID: "shared-" + value, ExpectedVersion: expected, At: at, + }) + if err != nil { + t.Fatalf("PutResource(%s): %v", orgID, err) + } + return got + } + put("acme", `{"v":"a1"}`, 0) + acme := put("acme", `{"v":"a2"}`, 1) + globex := put("globex", `{"v":"g1"}`, 0) + if acme.Version != 2 || globex.Version != 1 { + t.Fatalf("versions acme=%d globex=%d, want 2 and 1: orgs must not share a revision counter", acme.Version, globex.Version) + } + if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ + OrgID: "globex", Kind: "workflow-definitions", Value: []byte(`{}`), Actor: "operator", + Source: "test", RequestID: "stale", ExpectedVersion: 2, At: at, + }); !errors.Is(err, storecontract.ErrResourceVersionConflict) { + t.Fatalf("globex write expecting acme's version = %v, want ErrResourceVersionConflict", err) + } + + live, err := resources.Resource(t.Context(), "globex", "workflow-definitions") + if err != nil || string(live.Value) != `{"v":"g1"}` || live.OrgID != "globex" { + t.Fatalf("globex Resource = %+v, %v", live, err) + } + if _, err := resources.Resource(t.Context(), "initech", "workflow-definitions"); !errors.Is(err, storecontract.ErrResourceNotFound) { + t.Fatalf("an org that never wrote the kind reads %v, want ErrResourceNotFound", err) + } + history, err := resources.ResourceHistory(t.Context(), "acme", "workflow-definitions", 0) + if err != nil || len(history) != 2 { + t.Fatalf("acme history = %d entries, %v; want its own 2", len(history), err) + } + audit, err := resources.Audit(t.Context(), storecontract.AuditTableResources, + []string{storecontract.ResourceAuditKey("globex", "workflow-definitions")}, 0) + if err != nil { + t.Fatalf("Audit: %v", err) + } + for _, entry := range audit { + if entry.Version != 1 { + t.Fatalf("globex audit entry at version %d: acme's revisions leaked into its trail", entry.Version) + } + } + if len(audit) == 0 { + t.Fatal("globex audit is empty, want its first write") + } +} + +func TestPutResourceRefusesNoOrg(t *testing.T) { + if _, err := resourcesFor(t).PutResource(t.Context(), storecontract.ResourceWrite{ + Kind: "settings", Value: []byte(`{}`), Actor: "operator", Source: "test", RequestID: "r", + }); err == nil { + t.Fatal("a write naming no org was accepted") + } +} diff --git a/internal/webui/live_hub_test.go b/internal/webui/live_hub_test.go index 0dc326fb2..deb7801f3 100644 --- a/internal/webui/live_hub_test.go +++ b/internal/webui/live_hub_test.go @@ -30,7 +30,7 @@ import ( type liveResourceStore struct{ controlplane.ResourceStore } -func (*liveResourceStore) Resource(_ context.Context, kind string) (storecontract.Resource, error) { +func (*liveResourceStore) Resource(_ context.Context, _, kind string) (storecontract.Resource, error) { if kind == controlplane.PersonasKind { return storecontract.Resource{Kind: kind, Version: 1, Value: []byte(`{"default":"archie","personas":[]}`)}, nil } diff --git a/proto/controlplane/v1/controlplane.proto b/proto/controlplane/v1/controlplane.proto index e80093679..5315fab23 100644 --- a/proto/controlplane/v1/controlplane.proto +++ b/proto/controlplane/v1/controlplane.proto @@ -36,8 +36,14 @@ message Resource { int64 version = 2; bytes value_json = 3; google.protobuf.Timestamp updated_at = 4; + string org_id = 5; +} +// Every request that names a resource acts in one org's copy of it. An empty +// org_id is the default org. +message QueryRequest { + string kind = 1; + string org_id = 2; } -message QueryRequest { string kind = 1; } message QueryResponse { Resource resource = 1; } // Revision is one entry of a resource's audit trail: the value that version @@ -55,6 +61,7 @@ message HistoryRequest { // Limit caps the revisions returned, newest first. Zero means the server's // own default. int32 limit = 2; + string org_id = 3; } message HistoryResponse { repeated Revision revisions = 1; } @@ -92,12 +99,14 @@ message CommandRequest { // from browser/request JSON. string actor = 6; string source = 7; + string org_id = 8; } message CommandResponse { Resource resource = 1; } message WatchRequest { string kind = 1; int64 after_version = 2; + string org_id = 3; } message WatchResponse { Resource resource = 1;