Files
archie/internal/domain/access/store.go
T
sam 5c922038f9 fix(access): check the policy version before each decision
archied, archie-ui and the tool gateway re-read the stored policies on a 30s
ticker, so a forbid written seconds earlier did not stop a run. One
instance-wide revision now moves inside the transaction of every policy write,
and a decision reads it first and rebuilds the engine only when it moved.

A version read that fails, or a policy list that fails after a bump, refuses
with ErrChainUnavailable instead of standing on the last chain, so three call
sites now treat unavailable as availability rather than denial: dispatch leaves
the capture for the next cycle, a task start parks transient, and the tool
gateway reports the tool as unavailable.
2026-10-25 16:21:01 +11:00

64 lines
2.7 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"
"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)
}