fix(state): correct audit backfill scoping and policy write races

This commit is contained in:
sam committed 2026-10-25 20:59:04 +11:00
1 parent e3478ec400
commit b397329de0
6 files changed
+120 -45

No files matched your search

@@ -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)