From b397329de0dc293c7bd0cb77caed5ecc8028c70d Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 20:59:04 +1100 Subject: [PATCH] fix(state): correct audit backfill scoping and policy write races --- internal/domain/access/access.go | 17 +++++- internal/infrastructure/postgres/access.go | 42 +++++++------- internal/infrastructure/postgres/audit.go | 32 ++++++++--- .../migrations/0062_audit_read_model.sql | 56 +++++++++++++------ .../postgres/postgresdb/access.sql.go | 12 ++++ .../postgres/queries/access.sql | 6 ++ 6 files changed, 120 insertions(+), 45 deletions(-) diff --git a/internal/domain/access/access.go b/internal/domain/access/access.go index 4f9042da4..4583f8f45 100644 --- a/internal/domain/access/access.go +++ b/internal/domain/access/access.go @@ -120,12 +120,26 @@ type Policy struct { // (the Validator contract); this check is structural, so the store and the // reset path can reject a malformed document without an engine. func (p Policy) Validate() error { + if err := p.validateScope(); err != nil { + return err + } + if strings.TrimSpace(p.Text) == "" { + return fmt.Errorf("%w: no text", ErrInvalidPolicy) + } + return nil +} + +func (p Policy) validateScope() error { if strings.TrimSpace(p.ID) == "" { return fmt.Errorf("%w: no ID", ErrInvalidPolicy) } if err := p.Level.Validate(); err != nil { return err } + if strings.Contains(string(p.OrgID), "/") || strings.Contains(string(p.WorkspaceID), "/") { + // auditKey encodes the scope as path segments. + return fmt.Errorf("%w: org and workspace IDs must not contain a slash", ErrInvalidPolicy) + } switch p.Level { case LevelInstance: case LevelOrg: @@ -141,9 +155,6 @@ func (p Policy) Validate() error { return fmt.Errorf("%w: object policy carries no org, kind or ID", ErrInvalidPolicy) } } - if strings.TrimSpace(p.Text) == "" { - return fmt.Errorf("%w: no text", ErrInvalidPolicy) - } return nil } diff --git a/internal/infrastructure/postgres/access.go b/internal/infrastructure/postgres/access.go index 823bed920..5ace29efd 100644 --- a/internal/infrastructure/postgres/access.go +++ b/internal/infrastructure/postgres/access.go @@ -8,7 +8,6 @@ package postgres import ( "context" - "encoding/json" "errors" "fmt" "strings" @@ -54,12 +53,6 @@ func auditKey(p access.Policy) string { return strings.Trim(joined, "/") } -// textJSON wraps a policy text as the JSON string sys_audit stores. -func textJSON(text string) []byte { - out, _ := json.Marshal(text) - return out -} - // ListPolicies returns the stored policies the request's scope may see: // every policy across levels for an unscoped service. func (s *Store) ListPolicies(ctx context.Context) ([]access.Policy, error) { @@ -126,6 +119,9 @@ func (s *Store) PutPolicy(ctx context.Context, p access.Policy) (int64, error) { level, orgID, wsID, kind, id, policyID := accessScopeOf(p) var version int64 _, err := s.writePolicy(ctx, func(q *postgresdb.Queries) error { + if err := q.LockPolicyWrite(ctx, auditKey(p)); err != nil { + return err + } previous, err := q.AccessPolicyText(ctx, postgresdb.AccessPolicyTextParams{ Level: level, OrgID: orgID, WorkspaceID: wsID, ObjectKind: kind, ObjectID: id, PolicyID: policyID, @@ -133,10 +129,17 @@ func (s *Store) PutPolicy(ctx context.Context, p access.Policy) (int64, error) { var oldValue []byte switch { case err == nil: - oldValue = textJSON(previous) + oldValue, err = auditJSON(previous) + if err != nil { + return err + } case !errors.Is(err, pgx.ErrNoRows): return err } + newValue, err := auditJSON(p.Text) + if err != nil { + return err + } if err := q.UpsertAccessPolicy(ctx, postgresdb.UpsertAccessPolicyParams{ Level: level, OrgID: orgID, WorkspaceID: wsID, ObjectKind: kind, ObjectID: id, @@ -154,7 +157,7 @@ func (s *Store) PutPolicy(ctx context.Context, p access.Policy) (int64, error) { return q.InsertAccessPolicyAudit(ctx, postgresdb.InsertAccessPolicyAuditParams{ RecordKey: auditKey(p), OldValue: oldValue, - NewValue: textJSON(p.Text), + NewValue: newValue, RecordVersion: version, Actor: access.ActorFromContext(ctx), Source: access.SourceFromContext(ctx), @@ -169,9 +172,10 @@ func (s *Store) PutPolicy(ctx context.Context, p access.Policy) (int64, error) { } // auditOrg is the org a policy change belongs to. Instance-level policies -// govern the whole deployment, so they belong to the system org. +// govern the whole deployment, whatever org their scope field carries, so they +// belong to the system org. func auditOrg(p access.Policy) string { - if p.OrgID == "" { + if p.Level == access.LevelInstance || p.OrgID == "" { return storecontract.DefaultOrgID } return string(p.OrgID) @@ -185,6 +189,9 @@ func (s *Store) DeletePolicy(ctx context.Context, p access.Policy) error { } level, orgID, wsID, kind, id, policyID := accessScopeOf(p) _, err := s.writePolicy(ctx, func(q *postgresdb.Queries) error { + if err := q.LockPolicyWrite(ctx, auditKey(p)); err != nil { + return err + } oldText, err := q.DeleteAccessPolicy(ctx, postgresdb.DeleteAccessPolicyParams{ Level: level, OrgID: orgID, WorkspaceID: wsID, ObjectKind: kind, ObjectID: id, PolicyID: policyID, @@ -195,20 +202,17 @@ func (s *Store) DeletePolicy(ctx context.Context, p access.Policy) error { if err != nil { return err } + oldValue, err := auditJSON(oldText) + if err != nil { + return err + } version, err := q.UpsertAccessPolicyVersion(ctx, postgresdb.UpsertAccessPolicyVersionParams{ Level: level, OrgID: orgID, WorkspaceID: wsID, }) if err != nil { return err } - if err := q.InsertAccessPolicyAudit(ctx, postgresdb.InsertAccessPolicyAuditParams{ - RecordKey: auditKey(p), - OldValue: textJSON(oldText), - RecordVersion: version, - Actor: access.ActorFromContext(ctx), - Source: access.SourceFromContext(ctx), - OrgID: auditOrg(p), - }); err != nil { + if err := insertAudit(ctx, q, "access_policies", auditKey(p), "text", oldValue, nil, version, auditOrg(p)); err != nil { return err } // A level-scope with no policies left keeps no version row. diff --git a/internal/infrastructure/postgres/audit.go b/internal/infrastructure/postgres/audit.go index 62745816a..b3c326f29 100644 --- a/internal/infrastructure/postgres/audit.go +++ b/internal/infrastructure/postgres/audit.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "reflect" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb" @@ -29,15 +30,32 @@ func insertAudit(ctx context.Context, q *postgresdb.Queries, table, recordKey, f return nil } -// auditJSON encodes one side of an audit change. A nil value stays nil, which -// the column stores as NULL: absent is not the same fact as JSON null. -func auditJSON(value any) []byte { - if value == nil { - return nil +// auditJSON encodes one side of an audit change. An absent value stays nil, +// which the column stores as NULL: absent is not the same fact as JSON null. +// A value that cannot be encoded is an error, never a silent NULL that would +// read as a create or delete. +func auditJSON(value any) ([]byte, error) { + if isNil(value) { + return nil, nil } out, err := json.Marshal(value) if err != nil { - return nil + return nil, fmt.Errorf("audit value: %w", err) + } + return out, nil +} + +// isNil covers the typed nils interface comparison misses, so a nil pointer or +// map is recorded as absent rather than the JSON literal null. +func isNil(value any) bool { + if value == nil { + return true + } + v := reflect.ValueOf(value) + switch v.Kind() { + case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice: + return v.IsNil() + default: + return false } - return out } diff --git a/internal/infrastructure/postgres/migrations/0062_audit_read_model.sql b/internal/infrastructure/postgres/migrations/0062_audit_read_model.sql index fc3331956..c789a71f4 100644 --- a/internal/infrastructure/postgres/migrations/0062_audit_read_model.sql +++ b/internal/infrastructure/postgres/migrations/0062_audit_read_model.sql @@ -13,22 +13,46 @@ ALTER TABLE sys_audit ADD CONSTRAINT sys_audit_org_nonempty CHECK (org_id <> '') -- access_policies: /////; -- instance-level policies carry no org; reset rows are reset/instance or -- reset/org/. -UPDATE sys_audit SET org_id = - CASE table_name - WHEN 'resources' THEN - CASE - WHEN position('/' IN record_key) > 0 THEN split_part(record_key, '/', 1) - ELSE 'org-sys' - END - WHEN 'access_policies' THEN - CASE - WHEN record_key LIKE 'reset/org/%' THEN split_part(record_key, '/', 3) - WHEN record_key LIKE 'reset/%' THEN 'org-sys' - WHEN split_part(record_key, '/', 1) = 'instance' THEN 'org-sys' - ELSE COALESCE(NULLIF(split_part(record_key, '/', 2), ''), 'org-sys') - END - ELSE 'org-sys' - END; +-- +-- record_key was never rewritten by the org rename, so rows written while the +-- default org was still called "default" carry that dead id. They belong to +-- org-sys, which is what the rename made the same org. +UPDATE sys_audit SET org_id = 'org-sys' +WHERE table_name = 'access_policies' + AND (split_part(record_key, '/', 2) = 'default' OR record_key = 'reset/org/default'); + +-- Org ids may themselves contain '/', which the old validation allowed. Match +-- the longest existing org prefix rather than the first path segment, so +-- acme/dev never resolves to acme. +UPDATE sys_audit a SET org_id = ( + SELECT o.id FROM orgs o + WHERE a.record_key LIKE o.id || '/%' + ORDER BY length(o.id) DESC + LIMIT 1 +) +WHERE a.table_name = 'resources' + AND a.org_id = 'org-sys' + AND EXISTS (SELECT 1 FROM orgs o WHERE a.record_key LIKE o.id || '/%'); + +UPDATE sys_audit a SET org_id = ( + SELECT o.id FROM orgs o + WHERE a.record_key LIKE split_part(a.record_key, '/', 1) || '/' || o.id || '/%' + ORDER BY length(o.id) DESC + LIMIT 1 +) +WHERE a.table_name = 'access_policies' + AND a.org_id = 'org-sys' + AND split_part(a.record_key, '/', 1) IN ('org', 'workspace', 'object') + AND EXISTS ( + SELECT 1 FROM orgs o + WHERE a.record_key LIKE split_part(a.record_key, '/', 1) || '/' || o.id || '/%' + ); + +UPDATE sys_audit a SET org_id = o.id +FROM orgs o +WHERE a.table_name = 'access_policies' + AND a.org_id = 'org-sys' + AND a.record_key = 'reset/org/' || o.id; CREATE INDEX idx_sys_audit_org_at ON sys_audit (org_id, at DESC, id DESC); CREATE INDEX idx_sys_audit_org_record ON sys_audit (org_id, table_name, record_key, at DESC, id DESC); diff --git a/internal/infrastructure/postgres/postgresdb/access.sql.go b/internal/infrastructure/postgres/postgresdb/access.sql.go index 8799ced0b..cb028f5da 100644 --- a/internal/infrastructure/postgres/postgresdb/access.sql.go +++ b/internal/infrastructure/postgres/postgresdb/access.sql.go @@ -283,6 +283,18 @@ func (q *Queries) ListDenials(ctx context.Context, arg ListDenialsParams) ([]Acc return items, nil } +const lockPolicyWrite = `-- name: LockPolicyWrite :exec +SELECT pg_advisory_xact_lock(hashtextextended($1::text, 0)) +` + +// Serialises concurrent writes to one policy key. SELECT FOR UPDATE cannot +// lock a row that does not exist yet, so without this two concurrent creates +// both read no previous row and the second is audited as a create. +func (q *Queries) LockPolicyWrite(ctx context.Context, key string) error { + _, err := q.db.Exec(ctx, lockPolicyWrite, key) + return err +} + const recordDenial = `-- name: RecordDenial :exec INSERT INTO access_denials (org_id, principal, action, resource_kind, resource_id, level, policies) VALUES ($1, $2, $3, $4, $5, $6, $7) diff --git a/internal/infrastructure/postgres/queries/access.sql b/internal/infrastructure/postgres/queries/access.sql index f82d1381d..7dc5ca4a4 100644 --- a/internal/infrastructure/postgres/queries/access.sql +++ b/internal/infrastructure/postgres/queries/access.sql @@ -22,6 +22,12 @@ WHERE level = $1 AND org_id = $2 AND workspace_id = $3 AND object_kind = $4 AND object_id = $5 AND policy_id = $6 RETURNING text; +-- name: LockPolicyWrite :exec +-- Serialises concurrent writes to one policy key. SELECT FOR UPDATE cannot +-- lock a row that does not exist yet, so without this two concurrent creates +-- both read no previous row and the second is audited as a create. +SELECT pg_advisory_xact_lock(hashtextextended(sqlc.arg(key)::text, 0)); + -- name: AccessPolicyText :one SELECT text FROM access_policies WHERE level = $1 AND org_id = $2 AND workspace_id = $3 AND object_kind = $4