// Access policies and denial records: the Postgres implementation of // internal/domain/access's PolicyStore, Resetter and DenialStore. // Policy changes are versioned per level // scope and audited as one sys_audit row per change naming who made it; // every write also bumps the instance-wide revision in the same transaction, // which is what tells an authorizer whether the chain it holds is current. package postgres import ( "context" "errors" "fmt" "strings" "time" "github.com/jackc/pgx/v5" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb" ) var ( _ access.PolicyStore = (*Store)(nil) _ access.Resetter = (*Store)(nil) _ access.DenialStore = (*Store)(nil) _ access.DenialPruner = (*Store)(nil) ) // shippedOrgPolicyIDs lists an org's shipped role policy IDs: the seed's // conflict keys, and what an org reset keeps. func shippedOrgPolicyIDs() []string { ids := make([]string, 0, 4) for _, p := range access.ShippedOrgPolicies("") { ids = append(ids, p.ID) } return ids } // accessScopeOf is the scope tuple every access query carries. func accessScopeOf(p access.Policy) (string, string, string, string, string, string) { return string(p.Level), string(p.OrgID), string(p.WorkspaceID), string(p.ObjectKind), p.ObjectID, p.ID } // auditKey is the sys_audit record_key for one policy: its scope and ID, so // an operator can trace one policy document's changes. func auditKey(p access.Policy) string { joined := strings.Join([]string{ string(p.Level), string(p.OrgID), string(p.WorkspaceID), string(p.ObjectKind), p.ObjectID, p.ID, }, "/") return strings.Trim(joined, "/") } // The reset command runs on the State Store host with no authenticated caller, // so its own audit rows name the recovery rather than reading as "unknown". const ( resetActor = "archie-state-store access reset" resetSource = "archie-state-store" ) // policyFromResetRow is one policy a reset deleted, as the audit row needs it. func policyFromResetRow(level, orgID, wsID, kind, id, policyID, text string) access.Policy { return access.Policy{ ID: policyID, Level: access.Level(level), OrgID: org.OrgID(orgID), WorkspaceID: org.WorkspaceID(wsID), ObjectKind: access.ResourceKind(kind), ObjectID: id, Text: text, } } // auditResetDeletes writes one delete row per policy a reset removed, at the // same scope version a DeletePolicy would have written. The shipped policies // the reset then re-puts carry their own PutPolicy rows. func auditResetDeletes(ctx context.Context, q *postgresdb.Queries, policies []access.Policy) error { type scope struct { level, orgID, workspaceID string } versions := make(map[scope]int64, len(policies)) for _, p := range policies { level, orgID, wsID, _, _, _ := accessScopeOf(p) s := scope{level: level, orgID: orgID, workspaceID: wsID} version, ok := versions[s] if !ok { bumped, err := q.UpsertAccessPolicyVersion(ctx, postgresdb.UpsertAccessPolicyVersionParams{ Level: level, OrgID: orgID, WorkspaceID: wsID, }) if err != nil { return err } version = bumped versions[s] = bumped } oldValue, err := auditJSON(p.Text) if err != nil { return err } if err := insertAudit(ctx, q, "access_policies", auditKey(p), "text", oldValue, nil, version, auditOrg(p)); err != nil { return err } } for s := range versions { // A scope with no policies left keeps no version row, as DeletePolicy does. if err := q.DeleteAccessPolicyVersion(ctx, postgresdb.DeleteAccessPolicyVersionParams{ Level: s.level, OrgID: s.orgID, WorkspaceID: s.workspaceID, }); err != nil { return err } } return nil } // 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) { rows, err := s.queries().ListAccessPolicies(ctx, postgresdb.ListAccessPoliciesParams{ ScopeOrg: scopeOrg(ctx), InstanceScope: org.InstanceScope(ctx), }) if err != nil { return nil, err } out := make([]access.Policy, 0, len(rows)) for _, r := range rows { out = append(out, access.Policy{ ID: r.PolicyID, Level: access.Level(r.Level), OrgID: org.OrgID(r.OrgID), WorkspaceID: org.WorkspaceID(r.WorkspaceID), ObjectKind: access.ResourceKind(r.ObjectKind), ObjectID: r.ObjectID, Text: r.Text, }) } return out, nil } // writePolicy runs one policy mutation and the instance-wide version bump in // one transaction, and returns the new version. Every write path goes through // it so no path can move the policies without moving the version the // authorizers check: a mutation that lands without the bump would let a // process keep deciding on the chain that predates it. func (s *Store) writePolicy(ctx context.Context, write func(q *postgresdb.Queries) error) (int64, error) { var version int64 err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { q := s.queries().WithTx(tx) if err := write(q); err != nil { return err } bumped, err := q.BumpAccessPolicyRevision(ctx) if err != nil { return err } version = bumped return nil }) if err != nil { return 0, err } return version, nil } // PoliciesVersion returns the instance-wide policy version: it moves on every // policy write in the instance and only then, so an authorizer that reads it // before each decision can tell whether the chain it holds is current. func (s *Store) PoliciesVersion(ctx context.Context) (int64, error) { return s.queries().AccessPolicyRevision(ctx) } // PutPolicy stores or replaces one policy by its scope+ID key, bumps the // level's version, and writes the audit row -- in one transaction, so a // version that never moved cannot advertise a change that half-landed. func (s *Store) PutPolicy(ctx context.Context, p access.Policy) (int64, error) { if err := p.Validate(); err != nil { return 0, err } 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, }) var oldValue []byte switch { case err == nil: 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, PolicyID: policyID, Text: p.Text, }); err != nil { return err } v, err := q.UpsertAccessPolicyVersion(ctx, postgresdb.UpsertAccessPolicyVersionParams{ Level: level, OrgID: orgID, WorkspaceID: wsID, }) if err != nil { return err } version = v return q.InsertAccessPolicyAudit(ctx, postgresdb.InsertAccessPolicyAuditParams{ RecordKey: auditKey(p), OldValue: oldValue, NewValue: newValue, RecordVersion: version, Actor: access.ActorFromContext(ctx), Source: access.SourceFromContext(ctx), RequestID: "", OrgID: auditOrg(p), }) }) if err != nil { return 0, err } return version, nil } // auditOrg is the org a policy change belongs to. Instance-level policies // 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.Level == access.LevelInstance || p.OrgID == "" { return storecontract.DefaultOrgID } return string(p.OrgID) } // DeletePolicy removes one stored policy; an unknown key is // ErrPolicyNotFound. func (s *Store) DeletePolicy(ctx context.Context, p access.Policy) error { if err := p.Validate(); err != nil { return err } 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, }) if errors.Is(err, pgx.ErrNoRows) { return access.ErrPolicyNotFound } 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 := 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. return q.DeleteAccessPolicyVersion(ctx, postgresdb.DeleteAccessPolicyVersionParams{ Level: level, OrgID: orgID, WorkspaceID: wsID, }) }) return err } // EnsureShippedOrgPolicies seeds an org's shipped role policies when it // carries none of them, so a restart never overwrites an edited role policy. func (s *Store) EnsureShippedOrgPolicies(ctx context.Context, orgID org.OrgID) error { shipped := access.ShippedOrgPolicies(orgID) count, err := s.queries().CountShippedOrgPolicies(ctx, postgresdb.CountShippedOrgPoliciesParams{ OrgID: string(orgID), Column2: shippedOrgPolicyIDs(), }) if err != nil { return err } if count > 0 { return nil } for _, p := range shipped { if _, err := s.PutPolicy(ctx, p); err != nil { return err } } return nil } // ResetOrgPolicies restores the shipped role policies for one org and // removes its other org-level policies. It is the store-side half of // `archie-state-store access reset --org`, which runs on this host and records the // reset as an audit event. func (s *Store) ResetOrgPolicies(ctx context.Context, orgID org.OrgID) error { if orgID == "" { return fmt.Errorf("%w: no org", access.ErrInvalidPolicy) } ctx = access.WithActor(ctx, resetActor) ctx = access.WithSource(ctx, resetSource) if err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { q := postgresdb.New(tx) rows, err := q.DeleteOrgPoliciesNotShipped(ctx, postgresdb.DeleteOrgPoliciesNotShippedParams{ OrgID: string(orgID), Column2: shippedOrgPolicyIDs(), }) if err != nil { return err } policies := make([]access.Policy, 0, len(rows)) for _, r := range rows { policies = append(policies, policyFromResetRow(r.Level, r.OrgID, r.WorkspaceID, r.ObjectKind, r.ObjectID, r.PolicyID, r.Text)) } 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 } for _, p := range access.ShippedOrgPolicies(orgID) { if _, err := s.PutPolicy(ctx, p); err != nil { return err } } return nil } // ResetInstancePolicies removes every stored instance policy. The // cross-org forbid is engine-enforced and survives any reset. func (s *Store) ResetInstancePolicies(ctx context.Context) error { ctx = access.WithActor(ctx, resetActor) ctx = access.WithSource(ctx, resetSource) _, err := s.writePolicy(ctx, func(q *postgresdb.Queries) error { rows, err := q.ResetInstancePolicies(ctx) if err != nil { return err } policies := make([]access.Policy, 0, len(rows)) 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) }) return err } // RecordResetAudit records the one audit row a reset is written as. func (s *Store) RecordResetAudit(ctx context.Context, key, orgID string) error { return s.queries().InsertAccessResetAudit(ctx, postgresdb.InsertAccessResetAuditParams{RecordKey: key, OrgID: orgID}) } // RecordDenial records one refusal The store coalesces identical denials // within a minute into one row with a count, so a retry storm does not // flood the surface. func (s *Store) RecordDenial(ctx context.Context, d access.Denial) error { if d.Org == "" || d.Principal == "" { return fmt.Errorf("%w: org and principal are required", access.ErrInvalidPolicy) } return s.queries().RecordDenial(ctx, postgresdb.RecordDenialParams{ OrgID: string(d.Org), Principal: string(d.Principal), Action: string(d.Action), ResourceKind: string(d.Kind), ResourceID: d.ResourceID, Level: string(d.Level), Policies: d.Policies, }) } // ListDenials returns an org's most recent denials, newest first. func (s *Store) ListDenials(ctx context.Context, orgID org.OrgID, limit int) ([]access.Denial, error) { rows, err := s.queries().ListDenials(ctx, postgresdb.ListDenialsParams{ OrgID: string(orgID), Limit: clampInt32(limit), }) if err != nil { return nil, err } out := make([]access.Denial, 0, len(rows)) for _, r := range rows { out = append(out, access.Denial{ Principal: identity.IdentityID(r.Principal), Org: org.OrgID(r.OrgID), Action: access.Action(r.Action), Kind: access.ResourceKind(r.ResourceKind), ResourceID: r.ResourceID, Level: access.Level(r.Level), Policies: r.Policies, At: r.CreatedAt, Count: r.Count, }) } return out, nil } // denialPruneBatch is one delete's bound, so a single sweep does not own the // table for an unbounded time. const denialPruneBatch = 10_000 // PruneDenials deletes the denial records created before cutoff in bounded // batches and returns how many rows it removed. The State Store's retention // sweep is the only caller: the configured retention sets the cutoff. func (s *Store) PruneDenials(ctx context.Context, cutoff time.Time) (int64, error) { var total int64 for { removed, err := s.queries().PruneDenials(ctx, postgresdb.PruneDenialsParams{ Cutoff: cutoff.UTC(), Batch: denialPruneBatch, }) if err != nil { return total, err } total += removed if removed < denialPruneBatch { return total, nil } } }