Files
sam 003771ca48 feat(access): denial retention knob, State Store sweep and dashboard denials page (#1842)
Add [access].denial_retention (default 90 days, matching the audit-retention
seed) wired through config defaults and validation; the State Store sweeps
denial records older than the configured age hourly via the new
access.DenialPruner / PruneDenials store query. The dashboard gains
GET /api/access/denials (read_audit-gated, so org admins/owners see their
org's records; the store scopes by the credential's org) and an
/org/denials page backed by a Pinia store.
2026-10-31 14:23:31 +11:00

74 lines
3.1 KiB
Go

// The persistence contracts for policies and denial records. The State Store
// implements them (internal/infrastructure/postgres); composition wires them
// like org.Repository.
package access
import (
"context"
"time"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// PolicyStore persists stored policies. Versioned per level: PutPolicy
// returns the level's new version. PoliciesVersion carries the ordering
// guarantee the authorizers rely on: it moves on every write in the instance,
// in the same transaction as the write. Every change is an audit event naming
// who made it.
type PolicyStore interface {
// ListPolicies returns every stored policy across levels.
ListPolicies(ctx context.Context) ([]Policy, error)
// PoliciesVersion returns the instance-wide policy version: it changes on
// every policy write in the instance and only then, and never moves down,
// so an authorizer that reads it before each decision knows whether the
// chain it holds is current.
PoliciesVersion(ctx context.Context) (int64, error)
// PutPolicy stores or replaces one policy by its (level, scope, ID)
// key, and returns the level's new version.
PutPolicy(ctx context.Context, p Policy) (int64, error)
// DeletePolicy removes one stored policy; an unknown key is
// ErrPolicyNotFound.
DeletePolicy(ctx context.Context, p Policy) error
// EnsureShippedOrgPolicies seeds an org's shipped role policies when it
// has none; an org that already carries them is left untouched, so a
// restart never overwrites an edited role policy.
EnsureShippedOrgPolicies(ctx context.Context, orgID org.OrgID) error
}
// Resetter restores access for a locked-out org or a broken instance policy.
// It
// runs only on the State Store host, never over the network, and the reset
// command records it as an audit event.
type Resetter interface {
// ResetOrgPolicies restores the shipped role policies for one org and
// removes its other org-level policies.
ResetOrgPolicies(ctx context.Context, orgID org.OrgID) error
// ResetInstancePolicies removes every stored instance policy. The
// cross-org forbid is engine-enforced and survives any reset.
ResetInstancePolicies(ctx context.Context) error
}
// DenialRecorder records one refusal with the level that decided it and the
// deciding policies.
type DenialRecorder interface {
RecordDenial(ctx context.Context, d Denial) error
}
// DenialStore records and lists denial records. The store coalesces
// identical denials within a minute into one row with a count, so a retry
// storm does not flood the surface.
type DenialStore interface {
DenialRecorder
// ListDenials returns an org's most recent denials, newest first.
ListDenials(ctx context.Context, orgID org.OrgID, limit int) ([]Denial, error)
}
// DenialPruner deletes denial records older than a cutoff. The State Store's
// retention sweep is its only caller: the configured retention decides the
// cutoff, and this deletes nothing else.
type DenialPruner interface {
// PruneDenials deletes every denial record created before cutoff and
// returns how many rows it removed.
PruneDenials(ctx context.Context, cutoff time.Time) (int64, error)
}