114 lines
3.8 KiB
Go
114 lines
3.8 KiB
Go
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
|
|
denials DenialRecorder
|
|
log *slog.Logger
|
|
}
|
|
|
|
// Recording wraps a so every refusal is stored as a denial, whichever entry
|
|
// point asked. A decision that could not be made is not a denial and is not
|
|
// stored.
|
|
func Recording(a Authorizer, denials DenialRecorder, log *slog.Logger) Authorizer {
|
|
return recording{Authorizer: a, denials: denials, 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)
|
|
if d.Allowed || errors.Is(d.Err, ErrChainUnavailable) {
|
|
return d
|
|
}
|
|
err := r.denials.RecordDenial(context.WithoutCancel(ctx), Denial{
|
|
Principal: p.IdentityID, Org: p.Org, Action: action, Kind: res.Kind, ResourceID: res.ID,
|
|
Level: d.Level, Policies: d.Policies, At: time.Now(), Count: 1,
|
|
})
|
|
if err != nil {
|
|
r.log.ErrorContext(ctx, "record access denial", "principal", p.IdentityID, "action", action, "kind", res.Kind, "error", err)
|
|
}
|
|
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 denial, 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
|
|
}
|