refactor: rename denial records to access decisions
- rename governance.access_denials table, indexes and API types via migration 00061 - expose /api/access/decisions and decisions payload; keep audit auth unchanged - rename UI store, page and route from denials to decisions
This commit is contained in:
23 files changed
+187
-172
No files matched your search
@@ -367,10 +367,10 @@ const (
|
||||
OutcomeDenied Outcome = "denied"
|
||||
)
|
||||
|
||||
// Denial is one recorded authorization decision, allowed or denied according
|
||||
// DecisionRecord is one recorded authorization decision, allowed or denied according
|
||||
// to Outcome: the principal, action, resource and deciding policies. It never
|
||||
// holds request credentials.
|
||||
type Denial struct {
|
||||
type DecisionRecord struct {
|
||||
Principal IdentityID `json:"principal"`
|
||||
Org OrgID `json:"org"`
|
||||
Action Action `json:"action"`
|
||||
@@ -416,20 +416,20 @@ type Resetter interface {
|
||||
ResetInstancePolicies(ctx context.Context) error
|
||||
}
|
||||
|
||||
// DenialRecorder records one access refusal.
|
||||
type DenialRecorder interface {
|
||||
RecordDenial(ctx context.Context, d Denial) error
|
||||
// DecisionRecorder records one access refusal.
|
||||
type DecisionRecorder interface {
|
||||
RecordDecision(ctx context.Context, d DecisionRecord) error
|
||||
}
|
||||
|
||||
// DenialStore records and lists coalesced denial records.
|
||||
type DenialStore interface {
|
||||
DenialRecorder
|
||||
ListDenials(ctx context.Context, orgID OrgID, limit int) ([]Denial, error)
|
||||
// DecisionStore records and lists coalesced decision records.
|
||||
type DecisionStore interface {
|
||||
DecisionRecorder
|
||||
ListDecisions(ctx context.Context, orgID OrgID, limit int) ([]DecisionRecord, error)
|
||||
}
|
||||
|
||||
// DenialPruner deletes denial records older than a cutoff.
|
||||
type DenialPruner interface {
|
||||
PruneDenials(ctx context.Context, cutoff time.Time) (int64, error)
|
||||
// DecisionPruner deletes decision records older than a cutoff.
|
||||
type DecisionPruner interface {
|
||||
PruneDecisions(ctx context.Context, cutoff time.Time) (int64, error)
|
||||
}
|
||||
|
||||
type (
|
||||
|
||||
@@ -21,20 +21,20 @@ type policyAdmin interface {
|
||||
DeletePolicy(context.Context, Policy) error
|
||||
}
|
||||
|
||||
// denialSearch reads the recorded decisions.
|
||||
type denialSearch interface {
|
||||
SearchDenials(context.Context, DenialQuery) ([]Denial, error)
|
||||
// decisionSearch reads the recorded decisions.
|
||||
type decisionSearch interface {
|
||||
SearchDecisions(context.Context, DecisionQuery) ([]DecisionRecord, error)
|
||||
}
|
||||
|
||||
// accessAPI serves the access policies and the record of decisions.
|
||||
type accessAPI struct {
|
||||
policies policyAdmin
|
||||
denials denialSearch
|
||||
log *slog.Logger
|
||||
policies policyAdmin
|
||||
decisions decisionSearch
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func (m *Module) access() *accessAPI {
|
||||
return &accessAPI{policies: m, denials: m, log: m.log}
|
||||
return &accessAPI{policies: m, decisions: m, log: m.log}
|
||||
}
|
||||
|
||||
type policiesBody struct {
|
||||
@@ -50,7 +50,7 @@ type policyInput struct {
|
||||
Body Policy
|
||||
}
|
||||
|
||||
type denialsInput struct {
|
||||
type decisionsInput struct {
|
||||
Principal string `query:"principal" doc:"Only decisions about this identity"`
|
||||
Action string `query:"action" doc:"Only decisions about this action"`
|
||||
Kind string `query:"kind" doc:"Only decisions about this resource kind"`
|
||||
@@ -61,9 +61,9 @@ type denialsInput struct {
|
||||
Offset int `query:"offset" default:"0" minimum:"0" doc:"How many of the newest matching decisions to skip"`
|
||||
}
|
||||
|
||||
type denialsBody struct {
|
||||
Denials []Denial `json:"denials"`
|
||||
More bool `json:"more" doc:"Whether older decisions match beyond this page"`
|
||||
type decisionsBody struct {
|
||||
Decisions []DecisionRecord `json:"decisions"`
|
||||
More bool `json:"more" doc:"Whether older decisions match beyond this page"`
|
||||
}
|
||||
|
||||
func (m *Module) registerPolicies(api huma.API, op func(id, method, path, summary string, status int) huma.Operation) {
|
||||
@@ -73,9 +73,9 @@ func (m *Module) registerPolicies(api huma.API, op func(id, method, path, summar
|
||||
httpapi.Register(api, httpapi.Authorized(ActionManagePolicies, KindPolicy), op("delete-access-policy", http.MethodDelete, "/api/access/policies", "Delete an access policy", http.StatusNoContent), a.deletePolicy)
|
||||
}
|
||||
|
||||
func (m *Module) registerDenials(api huma.API, op func(id, method, path, summary string, status int) huma.Operation) {
|
||||
func (m *Module) registerDecisions(api huma.API, op func(id, method, path, summary string, status int) huma.Operation) {
|
||||
a := m.access()
|
||||
httpapi.Register(api, httpapi.Authorized(ActionReadAudit, KindAudit), op("list-access-denials", http.MethodGet, "/api/access/denials", "List access policy decisions", 0), a.listDenials)
|
||||
httpapi.Register(api, httpapi.Authorized(ActionReadAudit, KindAudit), op("list-access-decisions", http.MethodGet, "/api/access/decisions", "List access policy decisions", 0), a.listDecisions)
|
||||
}
|
||||
|
||||
func (a *accessAPI) listPolicies(ctx context.Context, _ *struct{}) (*httpapi.Reply[policiesBody], error) {
|
||||
@@ -144,20 +144,20 @@ func (a *accessAPI) scoped(ctx context.Context, value Policy) (Policy, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (a *accessAPI) listDenials(ctx context.Context, in *denialsInput) (*httpapi.Reply[denialsBody], error) {
|
||||
func (a *accessAPI) listDecisions(ctx context.Context, in *decisionsInput) (*httpapi.Reply[decisionsBody], error) {
|
||||
p, err := caller(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
found, err := a.denials.SearchDenials(ctx, DenialQuery{
|
||||
found, err := a.decisions.SearchDecisions(ctx, DecisionQuery{
|
||||
Org: p.Org, Principal: IdentityID(in.Principal), Action: Action(in.Action), Kind: ResourceKind(in.Kind),
|
||||
Outcome: Outcome(in.Outcome), Since: in.Since, Until: in.Until, Limit: in.Limit + 1, Offset: in.Offset,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, a.failed(ctx, "read the denials", err)
|
||||
return nil, a.failed(ctx, "read the decisions", err)
|
||||
}
|
||||
more := len(found) > in.Limit
|
||||
return httpapi.OK(denialsBody{Denials: found[:min(len(found), in.Limit)], More: more}), nil
|
||||
return httpapi.OK(decisionsBody{Decisions: found[:min(len(found), in.Limit)], More: more}), nil
|
||||
}
|
||||
|
||||
// failed answers a policy failure with the status a person can act on: the
|
||||
|
||||
@@ -623,7 +623,7 @@ func (m *Module) capabilities(ctx context.Context, _ *struct{}) (*httpapi.Reply[
|
||||
// The access pages are offered to those the policy chain lets use them.
|
||||
sections["org_members"] = m.mayUse(ctx, p, ActionManageMembers, KindMember)
|
||||
sections["org_policies"] = m.mayUse(ctx, p, ActionManagePolicies, KindPolicy)
|
||||
sections["org_denials"] = m.mayUse(ctx, p, ActionReadAudit, KindAudit)
|
||||
sections["org_decisions"] = m.mayUse(ctx, p, ActionReadAudit, KindAudit)
|
||||
// Every member reads the organisation they belong to and its workspaces.
|
||||
sections["org"] = signedIn
|
||||
sections["org_workspaces"] = signedIn
|
||||
|
||||
@@ -25,7 +25,9 @@ func (f fixedAuthorizer) Authorize(context.Context, Principal, Action, Resource,
|
||||
|
||||
type brokenLog struct{}
|
||||
|
||||
func (brokenLog) RecordDenial(context.Context, Denial) error { return errors.New("database down") }
|
||||
func (brokenLog) RecordDecision(context.Context, DecisionRecord) error {
|
||||
return errors.New("database down")
|
||||
}
|
||||
|
||||
// TestEveryDecisionIsRecorded guards the audit trail: an allowed decision and a
|
||||
// refused one are each stored with the principal, action, resource and deciding
|
||||
@@ -35,23 +37,23 @@ func TestEveryDecisionIsRecorded(t *testing.T) {
|
||||
const signature = "sha256=feedface"
|
||||
quiet := slog.New(slog.DiscardHandler)
|
||||
workflow := Resource{Kind: KindWorkflow, ID: "triage", Org: DefaultOrgID}
|
||||
ask := func(ctx context.Context, d Decision, rec DenialRecorder) {
|
||||
ask := func(ctx context.Context, d Decision, rec DecisionRecorder) {
|
||||
Recording(fixedAuthorizer{d}, rec, quiet).Authorize(ctx, SharedTokenOwner(), ActionRun, workflow, AccessContext{Signature: signature})
|
||||
}
|
||||
deliver := func(ctx context.Context, d Decision, rec DenialRecorder) {
|
||||
deliver := func(ctx context.Context, d Decision, rec DecisionRecorder) {
|
||||
RecordingDelivery(fixedDelivery{d}, rec, quiet).AuthorizeDelivery(ctx, DefaultOrgID, "alerts", "203.0.113.9")
|
||||
}
|
||||
chain := Denial{Principal: SystemID, Org: DefaultOrgID, Action: ActionRun, Kind: KindWorkflow, ResourceID: "triage", Level: LevelOrg}
|
||||
delivery := Denial{Principal: SenderID, Org: DefaultOrgID, Action: ActionDeliver, Kind: KindSource, ResourceID: "alerts", Level: LevelInstance}
|
||||
with := func(d Denial, outcome Outcome, policy string) Denial {
|
||||
chain := DecisionRecord{Principal: SystemID, Org: DefaultOrgID, Action: ActionRun, Kind: KindWorkflow, ResourceID: "triage", Level: LevelOrg}
|
||||
delivery := DecisionRecord{Principal: SenderID, Org: DefaultOrgID, Action: ActionDeliver, Kind: KindSource, ResourceID: "alerts", Level: LevelInstance}
|
||||
with := func(d DecisionRecord, outcome Outcome, policy string) DecisionRecord {
|
||||
d.Outcome, d.Policies = outcome, []string{policy}
|
||||
return d
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
run func(context.Context, Decision, DenialRecorder)
|
||||
run func(context.Context, Decision, DecisionRecorder)
|
||||
in Decision
|
||||
want Denial
|
||||
want DecisionRecord
|
||||
}{
|
||||
{"an allowed request", ask, AllowedAt(LevelOrg, []string{"org-owner"}), with(chain, OutcomeAllowed, "org-owner")},
|
||||
{"a refused request", ask, DeniedAt(LevelOrg, []string{"org-viewer"}), with(chain, OutcomeDenied, "org-viewer")},
|
||||
@@ -60,7 +62,7 @@ func TestEveryDecisionIsRecorded(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
log := &denialLog{}
|
||||
log := &decisionLog{}
|
||||
tt.run(t.Context(), tt.in, log)
|
||||
if len(log.recorded) != 1 {
|
||||
t.Fatalf("records = %d, want 1", len(log.recorded))
|
||||
|
||||
@@ -55,7 +55,7 @@ func RequireWorkload(ctx context.Context, a Authorizer, action Action, r Resourc
|
||||
|
||||
type recording struct {
|
||||
Authorizer
|
||||
records DenialRecorder
|
||||
records DecisionRecorder
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
@@ -65,20 +65,20 @@ type recording struct {
|
||||
// An allowed decision whose record cannot be written is returned as
|
||||
// ErrChainUnavailable: access that leaves no trace is not granted. A refusal
|
||||
// stays a refusal when its record fails, and the failure is logged.
|
||||
func Recording(a Authorizer, records DenialRecorder, log *slog.Logger) Authorizer {
|
||||
func Recording(a Authorizer, records DecisionRecorder, log *slog.Logger) Authorizer {
|
||||
return recording{Authorizer: a, records: records, log: log}
|
||||
}
|
||||
|
||||
func (r recording) Authorize(ctx context.Context, p Principal, action Action, res Resource, c AccessContext) Decision {
|
||||
d := r.Authorizer.Authorize(ctx, p, action, res, c)
|
||||
return recordDecision(ctx, r.records, r.log, d, Denial{
|
||||
return recordDecision(ctx, r.records, r.log, d, DecisionRecord{
|
||||
Principal: p.IdentityID, Org: p.Org, Action: action, Kind: res.Kind, ResourceID: res.ID,
|
||||
})
|
||||
}
|
||||
|
||||
// recordDecision stores d as the outcome of the request described by rec and
|
||||
// returns the decision the caller may act on.
|
||||
func recordDecision(ctx context.Context, records DenialRecorder, log *slog.Logger, d Decision, rec Denial) Decision {
|
||||
func recordDecision(ctx context.Context, records DecisionRecorder, log *slog.Logger, d Decision, rec DecisionRecord) Decision {
|
||||
if errors.Is(d.Err, ErrChainUnavailable) {
|
||||
return d
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func recordDecision(ctx context.Context, records DenialRecorder, log *slog.Logge
|
||||
rec.Outcome = OutcomeAllowed
|
||||
}
|
||||
rec.Level, rec.Policies, rec.At, rec.Count = d.Level, d.Policies, time.Now(), 1
|
||||
err := records.RecordDenial(context.WithoutCancel(ctx), rec)
|
||||
err := records.RecordDecision(context.WithoutCancel(ctx), rec)
|
||||
if err == nil {
|
||||
return d
|
||||
}
|
||||
@@ -116,7 +116,7 @@ func (m *Module) Allow(ctx context.Context, action, kind string) error {
|
||||
}
|
||||
|
||||
// mayUse reports whether p's grants cover action on a kind of resource in its
|
||||
// org. It asks the chain without recording a denial, since a dashboard probing
|
||||
// org. It asks the chain without recording a decision, since a dashboard probing
|
||||
// which pages to offer has not been refused anything.
|
||||
func (m *Module) mayUse(ctx context.Context, p Principal, action Action, kind ResourceKind) bool {
|
||||
chain := m.authz()
|
||||
@@ -131,7 +131,7 @@ func (m *Module) mayUse(ctx context.Context, p Principal, action Action, kind Re
|
||||
|
||||
type recordingDelivery struct {
|
||||
DeliveryAuthorizer
|
||||
records DenialRecorder
|
||||
records DecisionRecorder
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
@@ -139,13 +139,13 @@ type recordingDelivery struct {
|
||||
// outcome, for webhook and listener intake alike. An allowed delivery whose
|
||||
// record cannot be written is returned as ErrChainUnavailable, as Recording
|
||||
// does.
|
||||
func RecordingDelivery(a DeliveryAuthorizer, records DenialRecorder, log *slog.Logger) DeliveryAuthorizer {
|
||||
func RecordingDelivery(a DeliveryAuthorizer, records DecisionRecorder, log *slog.Logger) DeliveryAuthorizer {
|
||||
return recordingDelivery{DeliveryAuthorizer: a, records: records, log: log}
|
||||
}
|
||||
|
||||
func (r recordingDelivery) AuthorizeDelivery(ctx context.Context, orgID OrgID, sourcePath, addr string) Decision {
|
||||
d := r.DeliveryAuthorizer.AuthorizeDelivery(ctx, orgID, sourcePath, addr)
|
||||
return recordDecision(ctx, r.records, r.log, d, Denial{
|
||||
return recordDecision(ctx, r.records, r.log, d, DecisionRecord{
|
||||
Principal: SenderID, Org: orgID, Action: ActionDeliver, Kind: KindSource, ResourceID: sourcePath,
|
||||
})
|
||||
}
|
||||
@@ -51,18 +51,18 @@ func TestShippedPoliciesLetTheWorkloadWork(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type denialLog struct{ recorded []Denial }
|
||||
type decisionLog struct{ recorded []DecisionRecord }
|
||||
|
||||
func (d *denialLog) RecordDenial(_ context.Context, denial Denial) error {
|
||||
d.recorded = append(d.recorded, denial)
|
||||
func (d *decisionLog) RecordDecision(_ context.Context, rec DecisionRecord) error {
|
||||
d.recorded = append(d.recorded, rec)
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestEnforcementFailsClosedAndRecordsRefusals guards the single seam every
|
||||
// entry point shares: a chain that is not loaded refuses with
|
||||
// ErrChainUnavailable, as does one with no authorizer at all, a refusal is
|
||||
// recorded as a denial, and an undecidable request is not.
|
||||
func TestEnforcementFailsClosedAndRecordsRefusals(t *testing.T) {
|
||||
// ErrChainUnavailable, as does one with no authorizer at all, every allowed or
|
||||
// refused request is recorded, and an undecidable request is not.
|
||||
func TestEnforcementFailsClosedAndRecordsDecisions(t *testing.T) {
|
||||
failing := func(context.Context) (int64, error) { return 0, errors.New("database down") }
|
||||
nothing := func(context.Context) ([]Policy, error) { return nil, nil }
|
||||
pending := NewPending(failing, nothing, slog.New(slog.DiscardHandler))
|
||||
@@ -72,30 +72,30 @@ func TestEnforcementFailsClosedAndRecordsRefusals(t *testing.T) {
|
||||
resource := Resource{Kind: KindWorkflow, ID: "triage", Org: DefaultOrgID}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
authorizer Authorizer
|
||||
who Principal
|
||||
wantErr error
|
||||
wantDenials int
|
||||
wantHTTPDeny error
|
||||
name string
|
||||
authorizer Authorizer
|
||||
who Principal
|
||||
wantErr error
|
||||
wantDecisions int
|
||||
wantHTTPDeny error
|
||||
}{
|
||||
{name: "a chain that is not loaded", authorizer: pending, who: admin, wantErr: ErrChainUnavailable, wantHTTPDeny: httpapi.ErrUnavailable},
|
||||
{name: "no authorizer", authorizer: nil, who: admin, wantErr: ErrChainUnavailable, wantHTTPDeny: httpapi.ErrUnavailable},
|
||||
{name: "a refusal", authorizer: loaded, who: viewer, wantErr: ErrDenied, wantDenials: 1, wantHTTPDeny: httpapi.ErrDenied},
|
||||
{name: "a grant", authorizer: loaded, who: admin, wantDenials: 1},
|
||||
{name: "a refusal", authorizer: loaded, who: viewer, wantErr: ErrDenied, wantDecisions: 1, wantHTTPDeny: httpapi.ErrDenied},
|
||||
{name: "a grant", authorizer: loaded, who: admin, wantDecisions: 1},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
denials := &denialLog{}
|
||||
log := &decisionLog{}
|
||||
var a Authorizer
|
||||
if tt.authorizer != nil {
|
||||
a = Recording(tt.authorizer, denials, slog.New(slog.DiscardHandler))
|
||||
a = Recording(tt.authorizer, log, slog.New(slog.DiscardHandler))
|
||||
}
|
||||
if err := Require(t.Context(), a, tt.who, ActionRun, resource); !errors.Is(err, tt.wantErr) {
|
||||
t.Fatalf("Require = %v, want %v", err, tt.wantErr)
|
||||
}
|
||||
if len(denials.recorded) != tt.wantDenials {
|
||||
t.Fatalf("denials recorded = %d, want %d", len(denials.recorded), tt.wantDenials)
|
||||
if len(log.recorded) != tt.wantDecisions {
|
||||
t.Fatalf("decisions recorded = %d, want %d", len(log.recorded), tt.wantDecisions)
|
||||
}
|
||||
|
||||
m := &Module{}
|
||||
|
||||
@@ -57,8 +57,8 @@ type PolicyRow struct {
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
// DenialRow is a coalesced authorization decision in governance.access_denials.
|
||||
type DenialRow struct {
|
||||
// DecisionRow is a coalesced authorization decision in governance.access_decisions.
|
||||
type DecisionRow struct {
|
||||
ID int64 `db:"id"`
|
||||
OrgID string `db:"org_id"`
|
||||
Principal string `db:"principal"`
|
||||
@@ -279,36 +279,36 @@ func (s *Store) ResetInstancePolicies(ctx context.Context) error {
|
||||
})
|
||||
}
|
||||
|
||||
// RecordDenial stores or increments a per-minute coalesced access decision.
|
||||
func (s *Store) RecordDenial(ctx context.Context, d DenialRow) error {
|
||||
// RecordDecision stores or increments a per-minute coalesced access decision.
|
||||
func (s *Store) RecordDecision(ctx context.Context, d DecisionRow) error {
|
||||
policies := d.Policies
|
||||
if policies == nil {
|
||||
policies = []string{}
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO governance.access_denials (org_id, principal, action, resource_kind, resource_id, outcome, level, policies)
|
||||
INSERT INTO governance.access_decisions (org_id, principal, action, resource_kind, resource_id, outcome, level, policies)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (org_id, principal, action, resource_kind, resource_id, outcome, level, policies, minute_at)
|
||||
DO UPDATE SET count = governance.access_denials.count + 1, updated_at = now()`,
|
||||
DO UPDATE SET count = governance.access_decisions.count + 1, updated_at = now()`,
|
||||
d.OrgID, d.Principal, d.Action, d.ResourceKind, d.ResourceID, d.Outcome, d.Level, policies); err != nil {
|
||||
return fmt.Errorf("record denial: %w", err)
|
||||
return fmt.Errorf("record decision: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListDenials returns an org's most recent decisions, newest first.
|
||||
func (s *Store) ListDenials(ctx context.Context, orgID string, limit int) ([]DenialRow, error) {
|
||||
var out []DenialRow
|
||||
// ListDecisions returns an org's most recent decisions, newest first.
|
||||
func (s *Store) ListDecisions(ctx context.Context, orgID string, limit int) ([]DecisionRow, error) {
|
||||
var out []DecisionRow
|
||||
if err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT id, org_id, principal, action, resource_kind, resource_id, outcome, level, policies, minute_at, count, created_at, updated_at
|
||||
FROM governance.access_denials WHERE org_id = $1 ORDER BY id DESC LIMIT $2`, orgID, limit); err != nil {
|
||||
return nil, fmt.Errorf("list denials: %w", err)
|
||||
FROM governance.access_decisions WHERE org_id = $1 ORDER BY id DESC LIMIT $2`, orgID, limit); err != nil {
|
||||
return nil, fmt.Errorf("list decisions: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// DenialFilter narrows SearchDenials. A zero field matches everything.
|
||||
type DenialFilter struct {
|
||||
// DecisionFilter narrows SearchDecisions. A zero field matches everything.
|
||||
type DecisionFilter struct {
|
||||
OrgID string
|
||||
Principal string
|
||||
Action string
|
||||
@@ -319,12 +319,12 @@ type DenialFilter struct {
|
||||
Offset int
|
||||
}
|
||||
|
||||
// SearchDenials returns an org's decisions matching f, newest first.
|
||||
func (s *Store) SearchDenials(ctx context.Context, f DenialFilter) ([]DenialRow, error) {
|
||||
var out []DenialRow
|
||||
// SearchDecisions returns an org's decisions matching f, newest first.
|
||||
func (s *Store) SearchDecisions(ctx context.Context, f DecisionFilter) ([]DecisionRow, error) {
|
||||
var out []DecisionRow
|
||||
if err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT id, org_id, principal, action, resource_kind, resource_id, outcome, level, policies, minute_at, count, created_at, updated_at
|
||||
FROM governance.access_denials
|
||||
FROM governance.access_decisions
|
||||
WHERE org_id = $1
|
||||
AND ($2 = '' OR principal = $2)
|
||||
AND ($3 = '' OR action = $3)
|
||||
@@ -334,7 +334,7 @@ func (s *Store) SearchDenials(ctx context.Context, f DenialFilter) ([]DenialRow,
|
||||
AND ($7::timestamptz IS NULL OR created_at < $7)
|
||||
ORDER BY id DESC LIMIT $8 OFFSET $9`,
|
||||
f.OrgID, f.Principal, f.Action, f.ResourceKind, f.Outcome, nullTime(f.Since), nullTime(f.Until), f.Limit, f.Offset); err != nil {
|
||||
return nil, fmt.Errorf("search denials: %w", err)
|
||||
return nil, fmt.Errorf("search decisions: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -346,18 +346,18 @@ func nullTime(t time.Time) *time.Time {
|
||||
return &t
|
||||
}
|
||||
|
||||
// PruneDenials deletes up to batch denial rows created before cutoff.
|
||||
func (s *Store) PruneDenials(ctx context.Context, cutoff time.Time, batch int) (int64, error) {
|
||||
// PruneDecisions deletes up to batch decision rows created before cutoff.
|
||||
func (s *Store) PruneDecisions(ctx context.Context, cutoff time.Time, batch int) (int64, error) {
|
||||
res, err := s.db.ExecContext(ctx, `
|
||||
DELETE FROM governance.access_denials
|
||||
DELETE FROM governance.access_decisions
|
||||
WHERE id IN (
|
||||
SELECT id FROM governance.access_denials
|
||||
SELECT id FROM governance.access_decisions
|
||||
WHERE created_at < $1
|
||||
ORDER BY created_at
|
||||
LIMIT $2
|
||||
)`, cutoff.UTC(), batch)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("prune denials: %w", err)
|
||||
return 0, fmt.Errorf("prune decisions: %w", err)
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
@@ -117,7 +117,7 @@ func (m *Module) RegisterAccess(api huma.API) {
|
||||
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("suspend-org-member", http.MethodPost, "/api/orgs/{id}/members/{identity}/suspend", "Suspend a member's identity", http.StatusNoContent), a.suspend)
|
||||
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("reactivate-org-member", http.MethodPost, "/api/orgs/{id}/members/{identity}/reactivate", "Reactivate a member's identity", http.StatusNoContent), a.reactivate)
|
||||
m.registerPolicies(api, op)
|
||||
m.registerDenials(api, op)
|
||||
m.registerDecisions(api, op)
|
||||
}
|
||||
|
||||
func (a *memberAPI) list(ctx context.Context, in *orgPath) (*httpapi.Reply[membersBody], error) {
|
||||
|
||||
@@ -105,14 +105,14 @@ func TestAccessWritesNeedTheirGrant(t *testing.T) {
|
||||
if (mutates || candidates) && action != wantAction {
|
||||
t.Errorf("%s declares %q, want %q", route.ID, action, wantAction)
|
||||
}
|
||||
if !mutates && route.Path == "/api/access/denials" && action != string(ActionReadAudit) {
|
||||
if !mutates && route.Path == "/api/access/decisions" && action != string(ActionReadAudit) {
|
||||
t.Errorf("%s declares %q, want read_audit", route.ID, action)
|
||||
}
|
||||
resource := Resource{Kind: ResourceKind(kind), Org: DefaultOrgID}
|
||||
if !engine.Authorize(t.Context(), actor("admin", RoleAdmin), Action(action), resource, AccessContext{}).Allowed {
|
||||
t.Errorf("%s: an admin is refused %s %s", route.ID, action, kind)
|
||||
}
|
||||
if mutates || candidates || route.Path == "/api/access/denials" {
|
||||
if mutates || candidates || route.Path == "/api/access/decisions" {
|
||||
for _, role := range []Role{RoleViewer, RoleDeveloper} {
|
||||
if engine.Authorize(t.Context(), actor("lowly", role), Action(action), resource, AccessContext{}).Allowed {
|
||||
t.Errorf("%s: a %s is allowed %s %s", route.ID, role, action, kind)
|
||||
|
||||
@@ -97,7 +97,7 @@ func ShippedOrgPolicies(orgID OrgID) []Policy {
|
||||
return slices.Clip(append(ShippedRolePolicies(orgID), shippedExtraPolicies(orgID)...))
|
||||
}
|
||||
|
||||
// CrossOrgForbidID is the ID reported in denial records for cross-org refusals.
|
||||
// CrossOrgForbidID is the ID reported in decision records for cross-org refusals.
|
||||
const CrossOrgForbidID = PolicyCrossOrgID
|
||||
|
||||
// CrossOrgForbidText describes the non-removable cross-org forbid enforced
|
||||
|
||||
@@ -10,17 +10,17 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
_ PolicyStore = (*Module)(nil)
|
||||
_ Resetter = (*Module)(nil)
|
||||
_ DenialStore = (*Module)(nil)
|
||||
_ DenialRecorder = (*Module)(nil)
|
||||
_ DenialPruner = (*Module)(nil)
|
||||
_ PolicyStore = (*Module)(nil)
|
||||
_ Resetter = (*Module)(nil)
|
||||
_ DecisionStore = (*Module)(nil)
|
||||
_ DecisionRecorder = (*Module)(nil)
|
||||
_ DecisionPruner = (*Module)(nil)
|
||||
)
|
||||
|
||||
const (
|
||||
denialPruneBatch = 10_000
|
||||
defaultDenials = 100
|
||||
maxDenials = 10_000
|
||||
decisionPruneBatch = 10_000
|
||||
defaultDecisions = 100
|
||||
maxDecisions = 10_000
|
||||
)
|
||||
|
||||
func shippedOrgPolicyIDs() []string {
|
||||
@@ -268,15 +268,15 @@ func (m *Module) ResetInstancePolicies(ctx context.Context) error {
|
||||
return m.store.ResetInstancePolicies(ctx)
|
||||
}
|
||||
|
||||
// RecordDenial persists one coalesced access decision, allowed or denied.
|
||||
func (m *Module) RecordDenial(ctx context.Context, d Denial) error {
|
||||
// RecordDecision persists one coalesced access decision, allowed or denied.
|
||||
func (m *Module) RecordDecision(ctx context.Context, d DecisionRecord) error {
|
||||
if d.Org == "" || d.Principal == "" {
|
||||
return fmt.Errorf("%w: org and principal are required", ErrInvalidPolicy)
|
||||
}
|
||||
if d.Outcome != OutcomeAllowed && d.Outcome != OutcomeDenied {
|
||||
return fmt.Errorf("%w: unknown outcome %q", ErrInvalidPolicy, d.Outcome)
|
||||
}
|
||||
return m.store.RecordDenial(ctx, store.DenialRow{
|
||||
return m.store.RecordDecision(ctx, store.DecisionRow{
|
||||
OrgID: string(d.Org),
|
||||
Principal: string(d.Principal),
|
||||
Action: string(d.Action),
|
||||
@@ -288,13 +288,13 @@ func (m *Module) RecordDenial(ctx context.Context, d Denial) error {
|
||||
})
|
||||
}
|
||||
|
||||
// ListDenials returns an org's most recent decisions, newest first.
|
||||
func (m *Module) ListDenials(ctx context.Context, orgID OrgID, limit int) ([]Denial, error) {
|
||||
return m.SearchDenials(ctx, DenialQuery{Org: orgID, Limit: limit})
|
||||
// ListDecisions returns an org's most recent decisions, newest first.
|
||||
func (m *Module) ListDecisions(ctx context.Context, orgID OrgID, limit int) ([]DecisionRecord, error) {
|
||||
return m.SearchDecisions(ctx, DecisionQuery{Org: orgID, Limit: limit})
|
||||
}
|
||||
|
||||
// DenialQuery narrows SearchDenials. A zero field matches everything.
|
||||
type DenialQuery struct {
|
||||
// DecisionQuery narrows SearchDecisions. A zero field matches everything.
|
||||
type DecisionQuery struct {
|
||||
Org OrgID
|
||||
Principal IdentityID
|
||||
Action Action
|
||||
@@ -306,28 +306,28 @@ type DenialQuery struct {
|
||||
Offset int
|
||||
}
|
||||
|
||||
// SearchDenials returns the decisions matching q, newest first, at most
|
||||
// maxDenials per call.
|
||||
func (m *Module) SearchDenials(ctx context.Context, q DenialQuery) ([]Denial, error) {
|
||||
// SearchDecisions returns the decisions matching q, newest first, at most
|
||||
// maxDecisions per call.
|
||||
func (m *Module) SearchDecisions(ctx context.Context, q DecisionQuery) ([]DecisionRecord, error) {
|
||||
if q.Limit <= 0 {
|
||||
q.Limit = defaultDenials
|
||||
} else if q.Limit > maxDenials {
|
||||
q.Limit = maxDenials
|
||||
q.Limit = defaultDecisions
|
||||
} else if q.Limit > maxDecisions {
|
||||
q.Limit = maxDecisions
|
||||
}
|
||||
rows, err := m.store.SearchDenials(ctx, store.DenialFilter{
|
||||
rows, err := m.store.SearchDecisions(ctx, store.DecisionFilter{
|
||||
OrgID: string(q.Org), Principal: string(q.Principal), Action: string(q.Action),
|
||||
ResourceKind: string(q.Kind), Outcome: string(q.Outcome), Since: q.Since, Until: q.Until, Limit: q.Limit, Offset: max(q.Offset, 0),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return denialsFromRows(rows), nil
|
||||
return decisionsFromRows(rows), nil
|
||||
}
|
||||
|
||||
func denialsFromRows(rows []store.DenialRow) []Denial {
|
||||
out := make([]Denial, 0, len(rows))
|
||||
func decisionsFromRows(rows []store.DecisionRow) []DecisionRecord {
|
||||
out := make([]DecisionRecord, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, Denial{
|
||||
out = append(out, DecisionRecord{
|
||||
Principal: IdentityID(r.Principal),
|
||||
Org: OrgID(r.OrgID),
|
||||
Action: Action(r.Action),
|
||||
@@ -343,16 +343,16 @@ func denialsFromRows(rows []store.DenialRow) []Denial {
|
||||
return out
|
||||
}
|
||||
|
||||
// PruneDenials deletes denial rows older than cutoff in bounded batches.
|
||||
func (m *Module) PruneDenials(ctx context.Context, cutoff time.Time) (int64, error) {
|
||||
// PruneDecisions deletes decision rows older than cutoff in bounded batches.
|
||||
func (m *Module) PruneDecisions(ctx context.Context, cutoff time.Time) (int64, error) {
|
||||
var total int64
|
||||
for {
|
||||
removed, err := m.store.PruneDenials(ctx, cutoff, denialPruneBatch)
|
||||
removed, err := m.store.PruneDecisions(ctx, cutoff, decisionPruneBatch)
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
total += removed
|
||||
if removed < denialPruneBatch {
|
||||
if removed < decisionPruneBatch {
|
||||
return total, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user