package governance import ( "context" "errors" "fmt" "log/slog" "time" "github.com/samcharles93/archie-core/internal/platform/httpapi" ) // ErrDenied is returned when the policy chain refuses an action. var ErrDenied = errors.New("access: not permitted") // WorkloadID is the identity background work acts as. const WorkloadID IdentityID = "workload" // WorkloadPrincipal returns the principal queued work acts as in org: trigger // dispatch, run workers and delivery workers. It holds only the workload role, // which the shipped workload policy grants a narrow set of actions, so // background work is decided by the same chain as any other caller. func WorkloadPrincipal(org OrgID) Principal { return Principal{ IdentityID: WorkloadID, Kind: KindSystem, Org: org, Memberships: []Membership{{IdentityID: WorkloadID, OrgID: org, Role: RoleWorkload}}, } } // Require asks a whether p may take action on r. It returns nil when allowed, // ErrChainUnavailable when a cannot decide, and an error wrapping ErrDenied // otherwise. A nil a cannot decide, so the action is refused. func Require(ctx context.Context, a Authorizer, p Principal, action Action, r Resource) error { if a == nil { return ErrChainUnavailable } d := a.Authorize(ctx, p, action, r, AccessContext{Time: time.Now()}) switch { case d.Allowed: return nil case errors.Is(d.Err, ErrChainUnavailable): return ErrChainUnavailable } return fmt.Errorf("%w: %s %s %s", ErrDenied, action, r.Kind, r.ID) } // RequireWorkload is Require for the workload principal of the operating // organisation, whose policies govern the work every module queues. func RequireWorkload(ctx context.Context, a Authorizer, action Action, r Resource) error { r.Org = DefaultOrgID return Require(ctx, a, WorkloadPrincipal(DefaultOrgID), action, r) } type recording struct { Authorizer records DecisionRecorder log *slog.Logger } // Recording wraps a so every decision is stored with its outcome, regardless // of the entry point. A decision that could not be made is not recorded. // // 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 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, 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 DecisionRecorder, log *slog.Logger, d Decision, rec DecisionRecord) Decision { if errors.Is(d.Err, ErrChainUnavailable) { return d } rec.Outcome = OutcomeDenied if d.Allowed { rec.Outcome = OutcomeAllowed } rec.Level, rec.Policies, rec.At, rec.Count = d.Level, d.Policies, time.Now(), 1 err := records.RecordDecision(context.WithoutCancel(ctx), rec) if err == nil { return d } log.ErrorContext(ctx, "record access decision", "principal", rec.Principal, "action", rec.Action, "kind", rec.Kind, "outcome", rec.Outcome, "error", err) if d.Allowed { return Decision{Level: d.Level, Policies: d.Policies, Err: ErrChainUnavailable} } return d } // Allow decides a request for the HTTP API's guard: the caller acting in ctx // against the live policy chain. func (m *Module) Allow(ctx context.Context, action, kind string) error { p, ok := PrincipalFromContext(ctx) if !ok { return httpapi.ErrDenied } err := Require(ctx, m.authz(), p, Action(action), Resource{Kind: ResourceKind(kind), Org: p.Org}) switch { case err == nil: return nil case errors.Is(err, ErrDenied): return httpapi.ErrDenied } return httpapi.ErrUnavailable } // mayUse reports whether p's grants cover action on a kind of resource in its // 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() if r, ok := chain.(recording); ok { chain = r.Authorizer } if chain == nil || p.IdentityID == "" { return false } return chain.Authorize(ctx, p, action, Resource{Kind: kind, Org: p.Org}, AccessContext{Time: time.Now()}).Allowed } type recordingDelivery struct { DeliveryAuthorizer records DecisionRecorder log *slog.Logger } // RecordingDelivery wraps a so that every delivery decision is stored with its // 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 DecisionRecorder, log *slog.Logger) DeliveryAuthorizer { return recordingDelivery{DeliveryAuthorizer: a, records: records, log: log} } func (r recordingDelivery) AuthorizeDelivery(ctx context.Context, orgID OrgID, sourceID, addr string) Decision { d := r.DeliveryAuthorizer.AuthorizeDelivery(ctx, orgID, sourceID, addr) return recordDecision(ctx, r.records, r.log, d, DecisionRecord{ Principal: SenderID, Org: orgID, Action: ActionDeliver, Kind: KindSource, ResourceID: sourceID, }) }