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.
This commit is contained in:
sam committed 2026-10-25 16:21:01 +11:00
1 parent e4560c9f44
commit 5c922038f9
25 files changed
+1228 -797

No files matched your search

+1 -2
View File
@@ -326,12 +326,11 @@ func (b *boot) openAccessChain(ctx context.Context) error {
if !ok {
return fmt.Errorf("state store policy reader unavailable")
}
live := infraaccess.NewPending(source.ListPolicies, b.log)
live := infraaccess.NewPending(ctx, source.PoliciesVersion, source.ListPolicies, b.log)
if err := live.Reload(ctx); err != nil {
b.log.Warn("workflow policies unavailable; execution remains denied", "err", err)
}
b.accessChain, b.accessLive = live, live
go live.Run(ctx, applystatus.RestampInterval)
return nil
}
+1 -2
View File
@@ -13,7 +13,6 @@ import (
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/applystatus"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/eventtype"
"github.com/samcharles93/archie-core/internal/gateway"
@@ -107,7 +106,7 @@ func (b *server) registerStandaloneTools(ctx context.Context) {
// registry, so chat turns and Kit harnesses (through the task-tool RPCs) both
// reach them, each in its own caller's org.
func (b *server) registerWorkflowTools(ctx context.Context) {
authz := infraaccess.StartLive(ctx, b.stateStore.ListPolicies, applystatus.RestampInterval, b.log)
authz := infraaccess.StartLive(ctx, b.stateStore.ListPolicies, b.stateStore.PoliciesVersion, b.log)
workflows := controlplanerpc.NewWorkflows(b.stateStore.ControlPlane())
entries := append(gateway.WorkflowTools(workflows, authz), gateway.IntegrationTools(integrationSource{Workflows: workflows, store: b.stateStore}, authz)...)
for _, entry := range entries {
-1
View File
@@ -125,7 +125,6 @@ func compose(ctx context.Context, d deps) *webui.Server {
// The policy chain: wired together or not at all.
srv.Refusals = refusalStore(d.Store)
srv.Access = d.Access
srv.ReloadAccess = d.Access.Reload
srv.Principals = d.Principals
srv.Denials = d.Denials
wireTaskLogs(d, srv)
+4 -5
View File
@@ -14,7 +14,6 @@ import (
"time"
"github.com/samcharles93/archie-core/internal/app/servicekit"
"github.com/samcharles93/archie-core/internal/domain/applystatus"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/presence"
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
@@ -28,11 +27,11 @@ import (
)
// buildAccessChain starts the live access engine over the stored policies.
// It refuses every request until Run's first load succeeds and keeps
// retrying, so a State Store or database that is down or slow at start
// closes the dashboard rather than opening it or stopping it.
// It refuses every request until the first load succeeds, so a State Store or
// database that is down or slow at start closes the dashboard rather than
// opening it or stopping it.
func buildAccessChain(ctx context.Context, store *staterpc.Client, log *slog.Logger) *infraaccess.Live {
return infraaccess.StartLive(ctx, store.ListPolicies, applystatus.RestampInterval, log)
return infraaccess.StartLive(ctx, store.ListPolicies, store.PoliciesVersion, log)
}
// Run serves the dashboard until ctx is cancelled. It resolves its own
File diff suppressed because it is too large. Load diff
@@ -155,6 +155,7 @@ const (
StateStoreService_WorkflowCallStatus_FullMethodName = "/state.v1.StateStoreService/WorkflowCallStatus"
StateStoreService_GetPrincipal_FullMethodName = "/state.v1.StateStoreService/GetPrincipal"
StateStoreService_ListPolicies_FullMethodName = "/state.v1.StateStoreService/ListPolicies"
StateStoreService_GetPoliciesVersion_FullMethodName = "/state.v1.StateStoreService/GetPoliciesVersion"
StateStoreService_PutPolicy_FullMethodName = "/state.v1.StateStoreService/PutPolicy"
StateStoreService_DeletePolicy_FullMethodName = "/state.v1.StateStoreService/DeletePolicy"
StateStoreService_EnsureShippedOrgPolicies_FullMethodName = "/state.v1.StateStoreService/EnsureShippedOrgPolicies"
@@ -437,6 +438,10 @@ type StateStoreServiceClient interface {
// it serves, and its memberships (internal/domain/access).
GetPrincipal(ctx context.Context, in *GetPrincipalRequest, opts ...grpc.CallOption) (*GetPrincipalResponse, error)
ListPolicies(ctx context.Context, in *ListPoliciesRequest, opts ...grpc.CallOption) (*ListPoliciesResponse, error)
// GetPoliciesVersion is the instance-wide policy version an authorizer reads
// before each decision to know whether the chain it holds is current. A
// caller must read it before ListPolicies, never after.
GetPoliciesVersion(ctx context.Context, in *GetPoliciesVersionRequest, opts ...grpc.CallOption) (*GetPoliciesVersionResponse, error)
PutPolicy(ctx context.Context, in *PutPolicyRequest, opts ...grpc.CallOption) (*PutPolicyResponse, error)
DeletePolicy(ctx context.Context, in *DeletePolicyRequest, opts ...grpc.CallOption) (*DeletePolicyResponse, error)
EnsureShippedOrgPolicies(ctx context.Context, in *EnsureShippedOrgPoliciesRequest, opts ...grpc.CallOption) (*EnsureShippedOrgPoliciesResponse, error)
@@ -1866,6 +1871,16 @@ func (c *stateStoreServiceClient) ListPolicies(ctx context.Context, in *ListPoli
return out, nil
}
func (c *stateStoreServiceClient) GetPoliciesVersion(ctx context.Context, in *GetPoliciesVersionRequest, opts ...grpc.CallOption) (*GetPoliciesVersionResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetPoliciesVersionResponse)
err := c.cc.Invoke(ctx, StateStoreService_GetPoliciesVersion_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *stateStoreServiceClient) PutPolicy(ctx context.Context, in *PutPolicyRequest, opts ...grpc.CallOption) (*PutPolicyResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(PutPolicyResponse)
@@ -2335,6 +2350,10 @@ type StateStoreServiceServer interface {
// it serves, and its memberships (internal/domain/access).
GetPrincipal(context.Context, *GetPrincipalRequest) (*GetPrincipalResponse, error)
ListPolicies(context.Context, *ListPoliciesRequest) (*ListPoliciesResponse, error)
// GetPoliciesVersion is the instance-wide policy version an authorizer reads
// before each decision to know whether the chain it holds is current. A
// caller must read it before ListPolicies, never after.
GetPoliciesVersion(context.Context, *GetPoliciesVersionRequest) (*GetPoliciesVersionResponse, error)
PutPolicy(context.Context, *PutPolicyRequest) (*PutPolicyResponse, error)
DeletePolicy(context.Context, *DeletePolicyRequest) (*DeletePolicyResponse, error)
EnsureShippedOrgPolicies(context.Context, *EnsureShippedOrgPoliciesRequest) (*EnsureShippedOrgPoliciesResponse, error)
@@ -2785,6 +2804,9 @@ func (UnimplementedStateStoreServiceServer) GetPrincipal(context.Context, *GetPr
func (UnimplementedStateStoreServiceServer) ListPolicies(context.Context, *ListPoliciesRequest) (*ListPoliciesResponse, error) {
return nil, status.Error(codes.Unimplemented, "method ListPolicies not implemented")
}
func (UnimplementedStateStoreServiceServer) GetPoliciesVersion(context.Context, *GetPoliciesVersionRequest) (*GetPoliciesVersionResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetPoliciesVersion not implemented")
}
func (UnimplementedStateStoreServiceServer) PutPolicy(context.Context, *PutPolicyRequest) (*PutPolicyResponse, error) {
return nil, status.Error(codes.Unimplemented, "method PutPolicy not implemented")
}
@@ -5296,6 +5318,24 @@ func _StateStoreService_ListPolicies_Handler(srv interface{}, ctx context.Contex
return interceptor(ctx, in, info, handler)
}
func _StateStoreService_GetPoliciesVersion_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetPoliciesVersionRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(StateStoreServiceServer).GetPoliciesVersion(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: StateStoreService_GetPoliciesVersion_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(StateStoreServiceServer).GetPoliciesVersion(ctx, req.(*GetPoliciesVersionRequest))
}
return interceptor(ctx, in, info, handler)
}
func _StateStoreService_PutPolicy_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(PutPolicyRequest)
if err := dec(in); err != nil {
@@ -6213,6 +6253,10 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
MethodName: "ListPolicies",
Handler: _StateStoreService_ListPolicies_Handler,
},
{
MethodName: "GetPoliciesVersion",
Handler: _StateStoreService_GetPoliciesVersion_Handler,
},
{
MethodName: "PutPolicy",
Handler: _StateStoreService_PutPolicy_Handler,
+8 -1
View File
@@ -613,7 +613,7 @@ func (d *Daemon) dispatchOneBinding(ctx context.Context, b binding.Binding, c st
// The chain decides dispatch: the binding's principal may `run` this
// workflow in its workspace, and the event's signature result and address
// travel with the request. A denial is terminal; an unavailable principal
// leaves the capture listed for the next cycle.
// or policy chain leaves the capture listed for the next cycle.
if !d.authorizeDispatch(ctx, b, c, target) {
return
}
@@ -664,6 +664,13 @@ func (d *Daemon) authorizeDispatch(ctx context.Context, b binding.Binding, c sto
if decision.Allowed {
return true
}
if errors.Is(decision.Err, access.ErrChainUnavailable) {
// Nothing was evaluated. Recording it would mark the pair evaluated for
// good, so a State Store blip would drop the capture instead of leaving
// it for the next cycle.
d.Log.Warn("binding dispatch: access chain unavailable; dispatch left for the next cycle", "binding", b.ID, "capture", c.ID, "err", decision.Err)
return false
}
if d.Denials != nil {
denial := access.Denial{
Principal: principal.IdentityID, Org: principal.Org, Action: access.ActionRun,
+28 -10
View File
@@ -43,12 +43,12 @@ func (d *Daemon) adoptWorkflowIdentity(ctx context.Context, task *workflow.Task)
d.parkBlockedTask(ctx, task, "workflow "+task.Workflow+": "+err.Error(), pinParkClass(err), parkBlocker(err))
return false
}
if !d.mayRunWorkflow(ctx, task, d.publisherIdentity(task.Identity)) {
d.parkBlockedTask(ctx, task, "the identity that started it may not run workflow "+task.Workflow, taskstate.ParkNeedsHuman, taskstate.BlockerPolicyDenied)
if allowed, err := d.mayRunWorkflow(ctx, task, d.publisherIdentity(task.Identity)); !allowed {
d.parkRefusedRun(ctx, task, "the identity that started it may not run workflow "+task.Workflow, err)
return false
}
if !d.mayRunWorkflow(ctx, task, runsAs) {
d.parkBlockedTask(ctx, task, "identity "+iface.Identity+" may not run workflow "+task.Workflow, taskstate.ParkNeedsHuman, taskstate.BlockerPolicyDenied)
if allowed, err := d.mayRunWorkflow(ctx, task, runsAs); !allowed {
d.parkRefusedRun(ctx, task, "identity "+iface.Identity+" may not run workflow "+task.Workflow, err)
return false
}
// In memory only: the stored identity stays the starter's, so every
@@ -125,15 +125,16 @@ func (d *Daemon) databaseExecutionIdentity(ctx context.Context, task *workflow.T
// mayRunWorkflow asks the policy chain whether id may run the task's
// workflow, recording a denial. No chain allows it, as every other check on
// an install that has not built one.
func (d *Daemon) mayRunWorkflow(ctx context.Context, task *workflow.Task, id identity.IdentityID) bool {
// an install that has not built one. The error reports a chain that could not
// be consulted, which is not a denial: nothing was evaluated.
func (d *Daemon) mayRunWorkflow(ctx context.Context, task *workflow.Task, id identity.IdentityID) (bool, error) {
if d.Access == nil || d.Principals == nil {
return true
return true, nil
}
principal, err := d.Principals.PrincipalFor(ctx, id)
if err != nil {
d.Log.Warn("workflow identity: principal unavailable", "task", task.ID, "identity", id, "err", err)
return false
return false, nil
}
resource := access.Resource{Kind: access.KindWorkflow, ID: task.Workflow, Org: task.Org, Workspace: task.Workspace}
if resource.Org == "" {
@@ -141,7 +142,12 @@ func (d *Daemon) mayRunWorkflow(ctx context.Context, task *workflow.Task, id ide
}
decision := d.Access.Authorize(principal, access.ActionRun, resource, access.Context{Signature: task.BindingSignature, Addr: task.BindingAddr})
if decision.Allowed {
return true
return true, nil
}
if errors.Is(decision.Err, access.ErrChainUnavailable) {
// Nothing was evaluated, so there is no denial to record and no
// operator to involve: the chain will answer once the store does.
return false, pinFailure{taskstate.ParkTransient, decision.Err}
}
if d.Denials != nil {
denial := access.Denial{
@@ -152,5 +158,17 @@ func (d *Daemon) mayRunWorkflow(ctx context.Context, task *workflow.Task, id ide
d.Log.Warn("workflow identity: record denial", "task", task.ID, "err", err)
}
}
return false
return false, nil
}
// parkRefusedRun parks a task whose workflow identity check did not permit it.
// A denial is the operator's to fix, but a chain that could not be read is a
// store that will answer again, so it parks transient rather than demanding a
// human for a blip.
func (d *Daemon) parkRefusedRun(ctx context.Context, task *workflow.Task, reason string, err error) {
class, blocker := taskstate.ParkNeedsHuman, taskstate.BlockerPolicyDenied
if err != nil {
class, blocker = pinParkClass(err), parkBlocker(err)
}
d.parkBlockedTask(ctx, task, reason, class, blocker)
}
+8 -2
View File
@@ -10,12 +10,18 @@ import (
)
// PolicyStore persists stored policies. Versioned per level: PutPolicy
// returns the level's new version, so a policy change is one version bump
// the engine's next load can detect. Every change is an audit event naming
// 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)
+13 -4
View File
@@ -2,6 +2,7 @@ package gateway
import (
"context"
"errors"
"fmt"
"github.com/samcharles93/archie-core/internal/domain/access"
@@ -12,7 +13,9 @@ import (
// the context carries: the person chatting, or a task's identity. It asks with
// the action and resource the dashboard asks for the same request, so a tool
// never reaches further than its caller could through the dashboard. A
// context with no principal is refused.
// context with no principal is refused, and so is a chain that cannot be
// read -- reported as unavailable rather than as a permission the caller
// lacks.
func guarded(authz access.Authorizer, action access.Action, kind access.ResourceKind, id func(map[string]any) string, entry tools.ToolEntry) tools.ToolEntry {
handler := entry.Handler
entry.Handler = func(ctx context.Context, input map[string]any) (any, error) {
@@ -24,10 +27,16 @@ func guarded(authz access.Authorizer, action access.Action, kind access.Resource
if id != nil {
resource.ID = id(input)
}
if !authz.Authorize(p, action, resource, access.Context{}).Allowed {
return nil, fmt.Errorf("%s: %s may not %s this %s", entry.Name, p.IdentityID, action, kind)
decision := authz.Authorize(p, action, resource, access.Context{})
if decision.Allowed {
return handler(ctx, input)
}
return handler(ctx, input)
if errors.Is(decision.Err, access.ErrChainUnavailable) {
// Nothing was evaluated. Telling the caller it may not do this would
// send it looking for a permission it already holds.
return nil, fmt.Errorf("%s: access policies unavailable, retry shortly", entry.Name)
}
return nil, fmt.Errorf("%s: %s may not %s this %s", entry.Name, p.IdentityID, action, kind)
}
return entry
}
+9 -4
View File
@@ -5,6 +5,7 @@ package access
import (
"fmt"
"sync"
cedar "github.com/cedar-policy/cedar-go"
"github.com/cedar-policy/cedar-go/types"
@@ -282,9 +283,13 @@ func (e *Engine) chain(p access.Principal, r access.Resource) []*levelSet {
return out
}
// schemaValidator returns the resolved schema, building it once. The schema
// is part of the shipped vocabulary and cannot change at runtime.
func schemaValidator() (*validate.Validator, error) {
// schemaValidator returns the resolved schema, built once for the process. The
// schema is part of the shipped vocabulary and cannot change at runtime, so
// parsing it per policy -- which every rebuild used to do -- cost one schema
// parse per policy instead of one per process. validate.Validator only reads
// the resolved schema, so rebuilds that run concurrently (this package's and
// the account path's) can share it.
var schemaValidator = sync.OnceValues(func() (*validate.Validator, error) {
var sch schema.Schema
if err := sch.UnmarshalCedar([]byte(schemaText)); err != nil {
return nil, err
@@ -294,4 +299,4 @@ func schemaValidator() (*validate.Validator, error) {
return nil, err
}
return validate.New(resolved), nil
}
})
+107 -48
View File
@@ -3,6 +3,7 @@ package access
import (
"context"
"log/slog"
"sync"
"sync/atomic"
"time"
@@ -10,29 +11,112 @@ import (
"github.com/samcharles93/archie-core/internal/domain/org"
)
// Live is an engine rebuilt from the stored policies, so a policy edited in
// the State Store reaches every process that authorizes without a restart.
// A failed reload keeps the last engine.
// versionTimeout bounds one check of the version and the policies. The
// Authorizer contract carries no context, so the check owns its deadline:
// without one a hung State Store would hang every decision in the process
// instead of refusing.
const versionTimeout = 5 * time.Second
// Live is an engine rebuilt from the stored policies whenever their version
// moves, so a policy edit is enforced by the next decision rather than by a
// reload interval. A check that cannot read the store refuses: the chain it
// holds may predate the edit, and standing on it would be exactly the allow
// the edit meant to remove.
type Live struct {
load func(context.Context) ([]access.Policy, error)
log *slog.Logger
// ctx is the process lifetime: no request's context reaches a check.
//
//nolint:containedctx // The Authorizer contract carries no context, so the check has nothing but the process's own to derive a deadline from.
ctx context.Context
version func(context.Context) (int64, error)
load func(context.Context) ([]access.Policy, error)
log *slog.Logger
engine atomic.Pointer[Engine]
// mu serialises rebuilds, so a burst of edits costs one rebuild per
// version observed rather than one per decision.
mu sync.Mutex
stamp atomic.Int64
}
// NewPending returns a chain whose engine refuses every request until Run or
// Reload loads the stored policies, so a process that starts before its State
// Store is closed rather than opened.
func NewPending(load func(context.Context) ([]access.Policy, error), log *slog.Logger) *Live {
l := &Live{load: load, log: log}
// NewPending returns a chain whose engine refuses every request until the
// stored policies load, so a process that starts before its State Store is
// closed rather than opened.
//
// The loader is called as the service, never as a principal: a list scoped to
// one org would cache that org's policies under the instance-wide version, and
// every other org's policies would stay missing for as long as the version
// holds.
func NewPending(ctx context.Context, version func(context.Context) (int64, error), load func(context.Context) ([]access.Policy, error), log *slog.Logger) *Live {
l := &Live{
ctx: ctx,
version: version,
load: func(ctx context.Context) ([]access.Policy, error) {
return load(access.WithoutPrincipal(ctx))
},
log: log,
}
l.engine.Store(pendingEngine)
// No version has been read yet, so the first decision loads even if the
// store reports zero.
l.stamp.Store(-1)
return l
}
// Ready reports whether the stored policies have loaded.
func (l *Live) Ready() bool { return l.engine.Load().Ready() }
// Reload rebuilds the engine from the store now.
// Reload rebuilds the engine from the store now, without waiting for a
// decision, so a process can report a store it could not read at start. It
// holds the same deadline a decision does: a decision that waits on a rebuild
// must not wait forever on a store that never answers.
func (l *Live) Reload(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, versionTimeout)
defer cancel()
l.mu.Lock()
defer l.mu.Unlock()
version, err := l.version(ctx)
if err != nil {
return err
}
return l.apply(ctx, version)
}
// current returns the engine for the version the store reports, rebuilding it
// when the version moved since the engine was built. A check that cannot read
// the store returns the pending engine, which refuses everything.
func (l *Live) current() *Engine {
ctx, cancel := context.WithTimeout(l.ctx, versionTimeout)
defer cancel()
// The version before the list, never after and never in the same
// transaction: stamped with a version read first, a rebuilt engine can only
// be newer than its stamp, which costs one more rebuild. The other order
// lets an engine hold content from before an edit while stamped as current,
// and no later check would ever see a version it had not already applied.
version, err := l.version(ctx)
if err != nil {
l.log.Warn("access policy version unavailable; refusing until it is readable", "err", err)
return pendingEngine
}
if version == l.stamp.Load() {
return l.engine.Load()
}
l.mu.Lock()
defer l.mu.Unlock()
// A decision that waited on the lock has already rebuilt for this version.
if version == l.stamp.Load() {
return l.engine.Load()
}
if err := l.apply(ctx, version); err != nil {
l.log.Warn("access policies not reloaded; refusing until they load", "err", err)
return pendingEngine
}
return l.engine.Load()
}
// apply loads the policies and swaps in the engine they describe, stamped with
// the version read before them. The caller holds mu.
func (l *Live) apply(ctx context.Context, version int64) error {
stored, err := l.load(ctx)
if err != nil {
return err
@@ -42,55 +126,30 @@ func (l *Live) Reload(ctx context.Context) error {
l.log.Error("stored access policy is invalid and denies its level",
"policy", problem.Policy.ID, "level", problem.Policy.Level, "err", problem.Err)
}
// The engine before the stamp: a check that reads the stamp must find the
// engine it names.
l.engine.Store(engine)
l.stamp.Store(version)
return nil
}
// Run loads at once, then reloads on every interval until ctx ends, so the
// first load does not wait an interval after the State Store returns.
func (l *Live) Run(ctx context.Context, interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
l.reload(ctx)
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
func (l *Live) reload(ctx context.Context) {
err := l.Reload(ctx)
if err == nil || ctx.Err() != nil {
return
}
if l.Ready() {
l.log.Warn("access policies not reloaded; keeping the last chain", "err", err)
return
}
l.log.Warn("access policies not loaded; requests are refused until they load", "err", err)
}
func (l *Live) Authorize(p access.Principal, a access.Action, r access.Resource, c access.Context) access.Decision {
return l.engine.Load().Authorize(p, a, r, c)
return l.current().Authorize(p, a, r, c)
}
func (l *Live) AuthorizeDelivery(orgID org.OrgID, sourcePath, addr string) access.Decision {
return l.engine.Load().AuthorizeDelivery(orgID, sourcePath, addr)
return l.current().AuthorizeDelivery(orgID, sourcePath, addr)
}
// Problems reports the current engine's invalid policies.
func (l *Live) Problems() []Problem { return l.engine.Load().Problems() }
// StartLive loads the stored policies through list and keeps them current
// until ctx ends. The chain decides for every org, so it loads as the
// service, never as the person whose policy edit triggered a reload.
func StartLive(ctx context.Context, list func(context.Context) ([]access.Policy, error), interval time.Duration, log *slog.Logger) *Live {
live := NewPending(func(ctx context.Context) ([]access.Policy, error) {
return list(access.WithoutPrincipal(ctx))
}, log)
go live.Run(ctx, interval)
// StartLive loads the stored policies through list and keeps them current as
// they change.
func StartLive(ctx context.Context, list func(context.Context) ([]access.Policy, error), version func(context.Context) (int64, error), log *slog.Logger) *Live {
live := NewPending(ctx, version, list, log)
if err := live.Reload(ctx); err != nil {
log.Warn("access policies not loaded; requests are refused until they load", "err", err)
}
return live
}
+93 -1
View File
@@ -13,7 +13,9 @@ import (
// is up refuses every request until the first load succeeds, then allows.
func TestPendingChainRefusesUntilLoaded(t *testing.T) {
loaded := false
live := NewPending(func(context.Context) ([]access.Policy, error) {
live := NewPending(t.Context(), func(context.Context) (int64, error) {
return 1, nil
}, func(context.Context) ([]access.Policy, error) {
if !loaded {
return nil, errors.New("store down")
}
@@ -50,3 +52,93 @@ func TestPendingChainRefusesUntilLoaded(t *testing.T) {
t.Fatalf("after load = %+v, want allowed", decision)
}
}
// fakePolicyStore is a State Store stub: the policy version, the policy list,
// and the read failures a policy write can land between.
type fakePolicyStore struct {
version int64
policies []access.Policy
versionErr error
listErr error
}
func (s *fakePolicyStore) PoliciesVersion(context.Context) (int64, error) {
if s.versionErr != nil {
return 0, s.versionErr
}
return s.version, nil
}
func (s *fakePolicyStore) ListPolicies(context.Context) ([]access.Policy, error) {
if s.listErr != nil {
return nil, s.listErr
}
return s.policies, nil
}
// TestLiveAppliesAPolicyEditBeforeTheNextDecision pins the ordering guarantee a
// policy edit gets: the next decision after a write returns is decided by the
// edited chain, and a store failure in between refuses rather than standing on
// the chain the process happens to hold.
func TestLiveAppliesAPolicyEditBeforeTheNextDecision(t *testing.T) {
// The process starts with no stored policies, which allow everything, so
// the forbid is the edit under test.
forbidAll := access.Policy{ID: "forbid-all", Level: access.LevelInstance, Text: `forbid(principal, action, resource);`}
storeDown := errors.New("state store down")
request := func(live *Live) access.Decision {
return live.Authorize(access.SharedTokenOwner(), access.ActionRead,
access.Resource{Kind: access.KindDashboard}, access.Context{})
}
tests := []struct {
name string
write func(store *fakePolicyStore)
wantAllowed bool
wantUnavailable bool
}{
{
name: "an unchanged version keeps the chain",
write: func(store *fakePolicyStore) { store.policies = []access.Policy{forbidAll} },
wantAllowed: true,
},
{
name: "a bumped version applies the forbid",
write: func(store *fakePolicyStore) { store.policies = []access.Policy{forbidAll}; store.version++ },
},
{
name: "a failed version read refuses",
write: func(store *fakePolicyStore) {
store.policies = []access.Policy{forbidAll}
store.version++
store.versionErr = storeDown
},
wantUnavailable: true,
},
{
// The chain held is stamped below the version, so it may predate
// the edit: keeping it is how a forbid survives a store blip.
name: "a failed list read after a bump refuses instead of keeping the old chain",
write: func(store *fakePolicyStore) { store.version++; store.listErr = storeDown },
wantUnavailable: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &fakePolicyStore{version: 1}
live := NewPending(t.Context(), store.PoliciesVersion, store.ListPolicies, slog.New(slog.DiscardHandler))
if err := live.Reload(t.Context()); err != nil {
t.Fatal(err)
}
if decision := request(live); !decision.Allowed {
t.Fatalf("before the edit = %+v, want allowed so the edit is what refuses", decision)
}
tt.write(store)
decision := request(live)
if decision.Allowed != tt.wantAllowed {
t.Fatalf("after the edit = %+v, want allowed %v", decision, tt.wantAllowed)
}
if unavailable := errors.Is(decision.Err, access.ErrChainUnavailable); unavailable != tt.wantUnavailable {
t.Fatalf("after the edit = %+v, want unavailable %v", decision, tt.wantUnavailable)
}
})
}
}
+61 -23
View File
@@ -1,7 +1,9 @@
// Access policies and denial records: the Postgres implementation of
// internal/domain/access's PolicyStore, Resetter and DenialStore.
// Policy changes are versioned per level
// scope and audited as one sys_audit row per change naming who made it.
// scope and audited as one sys_audit row per change naming who made it;
// every write also bumps the instance-wide revision in the same transaction,
// which is what tells an authorizer whether the chain it holds is current.
package postgres
import (
@@ -80,6 +82,38 @@ func (s *Store) ListPolicies(ctx context.Context) ([]access.Policy, error) {
return out, nil
}
// writePolicy runs one policy mutation and the instance-wide version bump in
// one transaction, and returns the new version. Every write path goes through
// it so no path can move the policies without moving the version the
// authorizers check: a mutation that lands without the bump would let a
// process keep deciding on the chain that predates it.
func (s *Store) writePolicy(ctx context.Context, write func(q *postgresdb.Queries) error) (int64, error) {
var version int64
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
q := s.queries().WithTx(tx)
if err := write(q); err != nil {
return err
}
bumped, err := q.BumpAccessPolicyRevision(ctx)
if err != nil {
return err
}
version = bumped
return nil
})
if err != nil {
return 0, err
}
return version, nil
}
// PoliciesVersion returns the instance-wide policy version: it moves on every
// policy write in the instance and only then, so an authorizer that reads it
// before each decision can tell whether the chain it holds is current.
func (s *Store) PoliciesVersion(ctx context.Context) (int64, error) {
return s.queries().AccessPolicyRevision(ctx)
}
// PutPolicy stores or replaces one policy by its scope+ID key, bumps the
// level's version, and writes the audit row -- in one transaction, so a
// version that never moved cannot advertise a change that half-landed.
@@ -87,11 +121,9 @@ func (s *Store) PutPolicy(ctx context.Context, p access.Policy) (int64, error) {
if err := p.Validate(); err != nil {
return 0, err
}
q := s.queries()
level, orgID, wsID, kind, id, policyID := accessScopeOf(p)
var version int64
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
q := q.WithTx(tx)
_, err := s.writePolicy(ctx, func(q *postgresdb.Queries) error {
if err := q.UpsertAccessPolicy(ctx, postgresdb.UpsertAccessPolicyParams{
Level: level, OrgID: orgID, WorkspaceID: wsID,
ObjectKind: kind, ObjectID: id,
@@ -128,25 +160,28 @@ func (s *Store) DeletePolicy(ctx context.Context, p access.Policy) error {
return err
}
level, orgID, wsID, kind, id, policyID := accessScopeOf(p)
deleted, err := s.queries().DeleteAccessPolicy(ctx, postgresdb.DeleteAccessPolicyParams{
Level: level, OrgID: orgID, WorkspaceID: wsID,
ObjectKind: kind, ObjectID: id, PolicyID: policyID,
})
if err != nil {
return err
}
if deleted == 0 {
return access.ErrPolicyNotFound
}
if _, err := s.queries().UpsertAccessPolicyVersion(ctx, postgresdb.UpsertAccessPolicyVersionParams{
Level: level, OrgID: orgID, WorkspaceID: wsID,
}); err != nil {
return err
}
// A level-scope with no policies left keeps no version row.
return s.queries().DeleteAccessPolicyVersion(ctx, postgresdb.DeleteAccessPolicyVersionParams{
Level: level, OrgID: orgID, WorkspaceID: wsID,
_, err := s.writePolicy(ctx, func(q *postgresdb.Queries) error {
deleted, err := q.DeleteAccessPolicy(ctx, postgresdb.DeleteAccessPolicyParams{
Level: level, OrgID: orgID, WorkspaceID: wsID,
ObjectKind: kind, ObjectID: id, PolicyID: policyID,
})
if err != nil {
return err
}
if deleted == 0 {
return access.ErrPolicyNotFound
}
if _, err := q.UpsertAccessPolicyVersion(ctx, postgresdb.UpsertAccessPolicyVersionParams{
Level: level, OrgID: orgID, WorkspaceID: wsID,
}); err != nil {
return err
}
// A level-scope with no policies left keeps no version row.
return q.DeleteAccessPolicyVersion(ctx, postgresdb.DeleteAccessPolicyVersionParams{
Level: level, OrgID: orgID, WorkspaceID: wsID,
})
})
return err
}
// EnsureShippedOrgPolicies seeds an org's shipped role policies when it
@@ -194,7 +229,10 @@ func (s *Store) 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.
func (s *Store) ResetInstancePolicies(ctx context.Context) error {
return s.queries().ResetInstancePolicies(ctx)
_, err := s.writePolicy(ctx, func(q *postgresdb.Queries) error {
return q.ResetInstancePolicies(ctx)
})
return err
}
// RecordResetAudit records the one audit row a reset is written as.
@@ -0,0 +1,18 @@
-- One instance-wide policy version, bumped inside the transaction of every
-- policy write, so a process that authorizes can tell before each decision
-- whether its engine is current. Per-level counters cannot carry that: an edit
-- outside the level that currently holds the highest version is invisible, a
-- delete can move the highest version down, and an instance reset writes no
-- version at all. A singleton row, because access_policy_versions.level only
-- admits the real levels.
-- +goose Up
CREATE TABLE access_policy_revision (
only_row boolean PRIMARY KEY DEFAULT true CHECK (only_row),
version bigint NOT NULL DEFAULT 1,
updated_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO access_policy_revision (only_row) VALUES (true);
-- +goose Down
DROP TABLE access_policy_revision;
@@ -11,6 +11,33 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const accessPolicyRevision = `-- name: AccessPolicyRevision :one
SELECT version FROM access_policy_revision
`
func (q *Queries) AccessPolicyRevision(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, accessPolicyRevision)
var version int64
err := row.Scan(&version)
return version, err
}
const bumpAccessPolicyRevision = `-- name: BumpAccessPolicyRevision :one
INSERT INTO access_policy_revision (only_row, version) VALUES (true, 1)
ON CONFLICT (only_row)
DO UPDATE SET version = access_policy_revision.version + 1, updated_at = now()
RETURNING version
`
// The instance-wide version. It moves inside the transaction of every policy
// write, so it changes exactly when the policy snapshot changes.
func (q *Queries) BumpAccessPolicyRevision(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, bumpAccessPolicyRevision)
var version int64
err := row.Scan(&version)
return version, err
}
const countShippedOrgPolicies = `-- name: CountShippedOrgPolicies :one
SELECT count(*) FROM access_policies
WHERE level = 'org' AND org_id = $1 AND policy_id = ANY($2::text[])
@@ -36,6 +36,12 @@ type AccessPolicy struct {
UpdatedAt time.Time
}
type AccessPolicyRevision struct {
OnlyRow bool
Version int64
UpdatedAt time.Time
}
type AccessPolicyVersion struct {
Level string
OrgID string
@@ -62,3 +62,14 @@ FROM access_denials WHERE org_id = $1 ORDER BY id DESC LIMIT $2;
-- name: InsertAccessResetAudit :exec
INSERT INTO sys_audit (at, table_name, record_key, field, record_version, actor, source, request_id)
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archied access reset', 'archied', '');
-- name: BumpAccessPolicyRevision :one
-- The instance-wide version. It moves inside the transaction of every policy
-- write, so it changes exactly when the policy snapshot changes.
INSERT INTO access_policy_revision (only_row, version) VALUES (true, 1)
ON CONFLICT (only_row)
DO UPDATE SET version = access_policy_revision.version + 1, updated_at = now()
RETURNING version;
-- name: AccessPolicyRevision :one
SELECT version FROM access_policy_revision;
@@ -16,7 +16,9 @@ import (
// once they do, without rebuilding the probe.
func TestProblemProbeFollowsTheChain(t *testing.T) {
up := false
chain := infraaccess.NewPending(func(context.Context) ([]access.Policy, error) {
chain := infraaccess.NewPending(t.Context(), func(context.Context) (int64, error) {
return 1, nil
}, func(context.Context) ([]access.Policy, error) {
if !up {
return nil, errors.New("store down")
}
@@ -113,6 +113,17 @@ func (s *server) ListPolicies(ctx context.Context, _ *pb.ListPoliciesRequest) (*
return &pb.ListPoliciesResponse{Policies: out}, nil
}
func (s *server) GetPoliciesVersion(ctx context.Context, _ *pb.GetPoliciesVersionRequest) (*pb.GetPoliciesVersionResponse, error) {
if s.deps.Policies == nil {
return nil, errPoliciesUnavailable
}
version, err := s.deps.Policies.PoliciesVersion(ctx)
if err != nil {
return nil, accessStatus(err)
}
return &pb.GetPoliciesVersionResponse{Version: version}, nil
}
func (s *server) PutPolicy(ctx context.Context, request *pb.PutPolicyRequest) (*pb.PutPolicyResponse, error) {
if s.deps.Policies == nil {
return nil, errPoliciesUnavailable
@@ -52,6 +52,14 @@ func (c *Client) ListPolicies(ctx context.Context) ([]access.Policy, error) {
return mapValues(r.Policies, accessPolicyValue), nil
}
func (c *Client) PoliciesVersion(ctx context.Context) (int64, error) {
r, err := c.client.GetPoliciesVersion(ctx, &pb.GetPoliciesVersionRequest{})
if err != nil {
return 0, unmapError(err)
}
return r.GetVersion(), nil
}
func (c *Client) PutPolicy(ctx context.Context, p access.Policy) (int64, error) {
r, err := c.client.PutPolicy(ctx, &pb.PutPolicyRequest{Policy: accessPolicyProto(p)})
if err != nil {
-11
View File
@@ -49,7 +49,6 @@ func (s *Server) handlePutPolicy(w http.ResponseWriter, r *http.Request) {
writePolicyError(w, err)
return
}
s.reloadAccess(r)
writeJSON(w, map[string]int64{"version": version})
}
@@ -62,19 +61,9 @@ func (s *Server) handleDeletePolicy(w http.ResponseWriter, r *http.Request) {
writePolicyError(w, err)
return
}
s.reloadAccess(r)
w.WriteHeader(http.StatusNoContent)
}
func (s *Server) reloadAccess(r *http.Request) {
if s.ReloadAccess == nil {
return
}
if err := s.ReloadAccess(r.Context()); err != nil && s.Log != nil {
s.Log.Warn("access chain not reloaded after a policy edit; the periodic reload will apply it", "err", err)
}
}
// decodePolicy reads a policy and pins it to the caller's org: no request
// field chooses the org, and instance policies are not edited here.
func (s *Server) decodePolicy(w http.ResponseWriter, r *http.Request) (access.Policy, bool) {
-6
View File
@@ -82,12 +82,6 @@ func (s *Server) handleOrgCreate(w http.ResponseWriter, r *http.Request) {
writeOrgError(w, err)
return
}
// The new org's role policies were seeded; load them so its owner can act.
if s.ReloadAccess != nil {
if err := s.ReloadAccess(r.Context()); err != nil {
s.logf("reload access after org create", "err", err)
}
}
w.WriteHeader(http.StatusCreated)
writeJSON(w, map[string]any{"org": created})
}
-3
View File
@@ -115,9 +115,6 @@ type Server struct {
// Policies is the stored access policy chain, behind the policies page.
// Nil answers 503.
Policies access.PolicyStore
// ReloadAccess rebuilds Access from the stored policies, so an edit here
// applies to the next request. Nil leaves the periodic reload.
ReloadAccess func(context.Context) error
// now is the clock applyState reads staleness against. Nil means
// time.Now; tests set it to pin a record's age.
now func() time.Time
+6
View File
@@ -278,6 +278,10 @@ service StateStoreService {
// it serves, and its memberships (internal/domain/access).
rpc GetPrincipal(GetPrincipalRequest) returns (GetPrincipalResponse);
rpc ListPolicies(ListPoliciesRequest) returns (ListPoliciesResponse);
// GetPoliciesVersion is the instance-wide policy version an authorizer reads
// before each decision to know whether the chain it holds is current. A
// caller must read it before ListPolicies, never after.
rpc GetPoliciesVersion(GetPoliciesVersionRequest) returns (GetPoliciesVersionResponse);
rpc PutPolicy(PutPolicyRequest) returns (PutPolicyResponse);
rpc DeletePolicy(DeletePolicyRequest) returns (DeletePolicyResponse);
rpc EnsureShippedOrgPolicies(EnsureShippedOrgPoliciesRequest) returns (EnsureShippedOrgPoliciesResponse);
@@ -1549,6 +1553,8 @@ message AccessPolicy {
}
message ListPoliciesRequest {}
message ListPoliciesResponse { repeated AccessPolicy policies = 1; }
message GetPoliciesVersionRequest {}
message GetPoliciesVersionResponse { int64 version = 1; }
message PutPolicyRequest { AccessPolicy policy = 1; }
message PutPolicyResponse { int64 version = 1; }
message DeletePolicyRequest { AccessPolicy policy = 1; }