From d8cd9ffec74bd73398f517bb4cc11784af1ed35b Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 21:24:50 +1100 Subject: [PATCH] fix(state): close residual audit coverage gaps --- internal/infrastructure/postgres/access.go | 8 +++- internal/infrastructure/postgres/edastore.go | 17 ++++----- internal/infrastructure/postgres/orgs.go | 39 ++++++++++++-------- internal/infrastructure/postgres/sources.go | 20 ++++++++-- 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/internal/infrastructure/postgres/access.go b/internal/infrastructure/postgres/access.go index fd104e08f..7fe839745 100644 --- a/internal/infrastructure/postgres/access.go +++ b/internal/infrastructure/postgres/access.go @@ -323,7 +323,13 @@ func (s *Store) ResetOrgPolicies(ctx context.Context, orgID org.OrgID) error { for _, r := range rows { policies = append(policies, policyFromResetRow(r.Level, r.OrgID, r.WorkspaceID, r.ObjectKind, r.ObjectID, r.PolicyID, r.Text)) } - return auditResetDeletes(ctx, q, policies) + if err := auditResetDeletes(ctx, q, policies); err != nil { + return err + } + // Commit the revision with the deletes: an authorizer must stop + // honouring a removed policy even if the re-puts never run. + _, err = q.BumpAccessPolicyRevision(ctx) + return err }); err != nil { return err } diff --git a/internal/infrastructure/postgres/edastore.go b/internal/infrastructure/postgres/edastore.go index 70c48d6df..fbc99cb0f 100644 --- a/internal/infrastructure/postgres/edastore.go +++ b/internal/infrastructure/postgres/edastore.go @@ -346,6 +346,11 @@ func (s *EDA) UpdateMapping(ctx context.Context, m mapping.Mapping) error { if err != nil { return err } + oldDoc := mappingRowAuditDoc(old) + newDoc := mappingAuditDoc(m.ID, m.Name, m.SourceHint, m.EventTypeID, encoded) + if reflect.DeepEqual(oldDoc, newDoc) { + return nil + } n, err := q.UpdateMapping(ctx, postgresdb.UpdateMappingParams{ ScopeOrg: scopeOrg(ctx), ID: m.ID, @@ -360,11 +365,6 @@ func (s *EDA) UpdateMapping(ctx context.Context, m mapping.Mapping) error { if n == 0 { return storecontract.ErrMappingNotFound } - oldDoc := mappingRowAuditDoc(old) - newDoc := mappingAuditDoc(m.ID, m.Name, m.SourceHint, m.EventTypeID, encoded) - if reflect.DeepEqual(oldDoc, newDoc) { - return nil - } return insertAuditValues(ctx, q, "mappings", m.ID, "mapping", oldDoc, newDoc, 0, old.Mapping.OrgID) }) if err != nil { @@ -560,7 +560,9 @@ func (s *EDA) UpdateBinding(ctx context.Context, b binding.Binding) error { if err != nil { return err } - unchanged := bindingEditableEqual(old, b, inputs) && old.Status == string(binding.StatusPendingApproval) + if bindingEditableEqual(old, b, inputs) && old.Status == string(binding.StatusPendingApproval) { + return nil + } n, err := q.UpdateBinding(ctx, postgresdb.UpdateBindingParams{ ScopeOrg: scopeOrg(ctx), ID: b.ID, @@ -581,9 +583,6 @@ func (s *EDA) UpdateBinding(ctx context.Context, b binding.Binding) error { if n == 0 { return storecontract.ErrBindingNotFound } - if unchanged { - return nil - } next := old.Version + 1 return insertAuditValues(ctx, q, "bindings", b.ID, "binding", bindingRowAuditDoc(old), diff --git a/internal/infrastructure/postgres/orgs.go b/internal/infrastructure/postgres/orgs.go index aed69c910..45b43646b 100644 --- a/internal/infrastructure/postgres/orgs.go +++ b/internal/infrastructure/postgres/orgs.go @@ -48,20 +48,24 @@ func (s *Store) CreateOrg(ctx context.Context, value org.Org, owner identity.Ide } return org.Org{}, err } - if err := q.InsertWorkspace(ctx, postgresdb.InsertWorkspaceParams{ - ID: string(org.DefaultWorkspaceID), OrgID: string(value.ID), Name: "Default", - }); err != nil { - return org.Org{}, err - } - if err := q.EnsureMembership(ctx, postgresdb.EnsureMembershipParams{ - IdentityID: string(owner), OrgID: string(value.ID), Role: string(org.RoleOwner), - }); err != nil { - return org.Org{}, err - } if err := insertAuditValues(ctx, q, "orgs", string(value.ID), "org", nil, map[string]string{"id": string(value.ID), "name": value.Name}, 0, string(value.ID)); err != nil { return org.Org{}, err } + workspace := org.Workspace{ID: org.DefaultWorkspaceID, OrgID: value.ID, Name: "Default"} + if err := q.InsertWorkspace(ctx, postgresdb.InsertWorkspaceParams{ + ID: string(workspace.ID), OrgID: string(workspace.OrgID), Name: workspace.Name, + }); err != nil { + return org.Org{}, err + } + if err := auditWorkspaceCreate(ctx, q, workspace); err != nil { + return org.Org{}, err + } + if err := ensureMembershipAudited(ctx, q, membership, postgresdb.EnsureMembershipParams{ + IdentityID: string(owner), OrgID: string(value.ID), Role: string(org.RoleOwner), + }); err != nil { + return org.Org{}, err + } return value, tx.Commit(ctx) } @@ -107,11 +111,7 @@ func (s *Store) CreateWorkspace(ctx context.Context, value org.Workspace) (org.W }); err != nil { return err } - return insertAuditValues(ctx, q, "workspaces", string(value.OrgID)+"/"+string(value.ID), "workspace", nil, - map[string]string{ - "id": string(value.ID), "org_id": string(value.OrgID), - "name": value.Name, "environment": value.Environment, - }, 0, string(value.OrgID)) + return auditWorkspaceCreate(ctx, q, value) }) if isUniqueViolation(err) { return org.Workspace{}, fmt.Errorf("%w: %s/%s", org.ErrWorkspaceExists, value.OrgID, value.ID) @@ -122,6 +122,15 @@ func (s *Store) CreateWorkspace(ctx context.Context, value org.Workspace) (org.W return value, nil } +// auditWorkspaceCreate writes the create row for a workspace. +func auditWorkspaceCreate(ctx context.Context, q *postgresdb.Queries, value org.Workspace) error { + return insertAuditValues(ctx, q, "workspaces", string(value.OrgID)+"/"+string(value.ID), "workspace", nil, + map[string]string{ + "id": string(value.ID), "org_id": string(value.OrgID), + "name": value.Name, "environment": value.Environment, + }, 0, string(value.OrgID)) +} + // workspaceFromRow maps the generated workspace row to the domain workspace. func workspaceFromRow(w postgresdb.Workspace) org.Workspace { return org.Workspace{ diff --git a/internal/infrastructure/postgres/sources.go b/internal/infrastructure/postgres/sources.go index 26461ab06..71aa20553 100644 --- a/internal/infrastructure/postgres/sources.go +++ b/internal/infrastructure/postgres/sources.go @@ -82,6 +82,20 @@ func (s *EDA) SetSourceSigning(ctx context.Context, path string, from, to source orgID := string(org.OrgFromContext(ctx)) return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { q := postgresdb.New(tx) + if from == to { + // No transition to write; still report a missing source or a + // caller whose idea of the current state is stale. + current, err := q.GetSource(ctx, postgresdb.GetSourceParams{Path: path, ScopeOrg: scopeOrg(ctx)}) + switch { + case errors.Is(err, pgx.ErrNoRows): + return storecontract.ErrSourceNotFound + case err != nil: + return fmt.Errorf("edastore: set source signing: %w", err) + case current.Signing != string(to): + return storecontract.ErrSourceSigningStale + } + return nil + } n, err := q.SetSourceSigning(ctx, postgresdb.SetSourceSigningParams{ Path: path, FromSigning: string(from), ToSigning: string(to), ScopeOrg: scopeOrg(ctx), }) @@ -89,9 +103,6 @@ func (s *EDA) SetSourceSigning(ctx context.Context, path string, from, to source return fmt.Errorf("edastore: set source signing: %w", err) } if n == 1 { - if from == to { - return nil - } return insertAuditValues(ctx, q, "sources", path, "signing", string(from), string(to), 0, orgID) } exists, err := q.SourceExists(ctx, path) @@ -154,6 +165,9 @@ func (s *EDA) SetSourceDeliveryHeader(ctx context.Context, path, header string) func (s *EDA) DeleteSource(ctx context.Context, path string) error { return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { q := postgresdb.New(tx) + if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: path}); err != nil { + return err + } // The read is scoped like the delete: another org's source is absent, // and its secret never leaves the database. params := postgresdb.GetSourceParams{Path: path, ScopeOrg: scopeOrg(ctx)}