From afaea346ee93e7f2a9252536e574b416d3f63496 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 13 Nov 2026 03:54:05 +1100 Subject: [PATCH] refactor(access): authorization takes the caller's context instead of a stored one (#1925) --- internal/app/archied/bootstrap.go | 2 +- internal/app/archied/repository_check.go | 2 +- internal/app/archiegateway/gateway.go | 2 +- internal/app/archiegateway/mcp_reconcile.go | 4 +- internal/app/archiegateway/tools.go | 42 +++++++++---------- internal/app/statestore/policy_guard.go | 2 +- internal/daemon/daemon.go | 2 +- internal/daemon/workflow_identity.go | 2 +- internal/daemon/workflow_identity_test.go | 8 ++-- internal/domain/access/authorizer.go | 6 ++- internal/gateway/tool_access.go | 2 +- internal/gateway/tool_access_test.go | 2 +- .../infrastructure/access/delivery_test.go | 2 +- internal/infrastructure/access/engine.go | 5 ++- internal/infrastructure/access/engine_test.go | 6 +-- internal/infrastructure/access/live.go | 35 +++++++--------- internal/infrastructure/access/live_test.go | 10 ++--- .../infrastructure/captureintake/org_test.go | 2 +- .../infrastructure/captureintake/receiver.go | 2 +- .../infrastructure/readiness/probes_test.go | 2 +- internal/infrastructure/staterpc/accounts.go | 2 +- internal/webui/authorize.go | 4 +- internal/webui/authorize_test.go | 4 +- internal/webui/orgs_test.go | 2 +- internal/webui/task_recovery.go | 2 +- 25 files changed, 77 insertions(+), 77 deletions(-) diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 898056917..2e61ad7fe 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -317,7 +317,7 @@ func (b *boot) openAccessChain(ctx context.Context) error { if !ok { return fmt.Errorf("state store policy reader unavailable") } - live := infraaccess.NewPending(ctx, source.PoliciesVersion, source.ListPolicies, b.log) + live := infraaccess.NewPending(source.PoliciesVersion, source.ListPolicies, b.log) if err := live.Reload(ctx); err != nil { b.log.WarnContext(ctx, "workflow policies unavailable; execution remains denied", "err", err) } diff --git a/internal/app/archied/repository_check.go b/internal/app/archied/repository_check.go index c6edcee0b..0419710cd 100644 --- a/internal/app/archied/repository_check.go +++ b/internal/app/archied/repository_check.go @@ -37,7 +37,7 @@ func (s *repositoryServer) CheckRepository(ctx context.Context, r *pb.CheckRepos if b.d == nil || b.d.Access == nil || b.d.Principals == nil { return nil, status.Error(codes.Unavailable, "Repository checks are temporarily unavailable.") } - decision := b.d.Access.Authorize(caller, access.ActionUpdate, access.Resource{Kind: access.KindPolicy, ID: "repository-policies", Org: caller.Org}, access.Context{}) + decision := b.d.Access.Authorize(ctx, caller, access.ActionUpdate, access.Resource{Kind: access.KindPolicy, ID: "repository-policies", Org: caller.Org}, access.Context{}) if !decision.Allowed { return nil, status.Error(codes.PermissionDenied, "You do not have permission to check repository access.") } diff --git a/internal/app/archiegateway/gateway.go b/internal/app/archiegateway/gateway.go index 5a9f53b0d..5d3a3a6d1 100644 --- a/internal/app/archiegateway/gateway.go +++ b/internal/app/archiegateway/gateway.go @@ -48,7 +48,7 @@ func (b *server) startGatewayRuntime(ctx context.Context, actor gateway.ChatTask if err != nil { return nil, err } - if err := b.registerTools(); err != nil { + if err := b.registerTools(ctx); err != nil { return nil, err } b.registerStandaloneTools(ctx) diff --git a/internal/app/archiegateway/mcp_reconcile.go b/internal/app/archiegateway/mcp_reconcile.go index 1c226c70f..4f1b6c95f 100644 --- a/internal/app/archiegateway/mcp_reconcile.go +++ b/internal/app/archiegateway/mcp_reconcile.go @@ -53,8 +53,8 @@ func (b *server) reconcileToolSettings(ctx context.Context, cfg config.Config) e // API key again as part of the rebuild, so a rotated credential // applies too. b.toolReg.Unregister(webfetch.ToolName, minimax.ToolName) - b.registerWebFetchTool(cfg) - b.registerMinimaxTool(cfg, b.log) + b.registerWebFetchTool(ctx, cfg) + b.registerMinimaxTool(ctx, cfg, b.log) } return errors.Join(problems...) } diff --git a/internal/app/archiegateway/tools.go b/internal/app/archiegateway/tools.go index c7c03460e..80bebae4b 100644 --- a/internal/app/archiegateway/tools.go +++ b/internal/app/archiegateway/tools.go @@ -30,7 +30,7 @@ import ( "github.com/samcharles93/archie-core/internal/tools/webfetch" ) -func (b *server) registerTools() error { +func (b *server) registerTools(ctx context.Context) error { cfg, log := b.cfgHolder.Get(), b.log b.providerRegistry = toolprovider.NewRegistry(b.toolReg) b.mcpMu.Lock() @@ -41,27 +41,27 @@ func (b *server) registerTools() error { // A workspace chosen in the dashboard names a directory nobody has // created yet; every file tool would fail on it. if err := os.MkdirAll(workspace, 0o700); err != nil { - log.Error("create chat workspace", "workspace", workspace, "err", err) + log.ErrorContext(ctx, "create chat workspace", "workspace", workspace, "err", err) } if err := b.providerRegistry.Register(builtintoolprovider.New(workspace, unrestricted)); err != nil { - log.Error("workspace tool provider registration failed", "err", err) + log.ErrorContext(ctx, "workspace tool provider registration failed", "err", err) return err } - log.Info("workspace tools enabled", + log.InfoContext(ctx, "workspace tools enabled", "workspace", workspace, "unrestricted_filesystem", unrestricted) } else { - log.Info("workspace tools disabled (chat.workspace is unset)") + log.InfoContext(ctx, "workspace tools disabled (chat.workspace is unset)") } for _, srv := range cfg.Tools.MCPServers { provider, err := b.buildMCPProvider(srv) if err != nil { - log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err) + log.WarnContext(ctx, "mcp tool provider skipped", "name", srv.Name, "err", err) continue } if err := b.providerRegistry.RegisterOptional(provider); err != nil { - log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err) + log.WarnContext(ctx, "mcp tool provider skipped", "name", srv.Name, "err", err) continue } b.mcpMu.Lock() @@ -69,7 +69,7 @@ func (b *server) registerTools() error { b.mcpMu.Unlock() } if err := b.capabilityHost.Register(b.providerRegistry); err != nil { - log.Error("tool-provider capability registration failed", "err", err) + log.ErrorContext(ctx, "tool-provider capability registration failed", "err", err) return err } return nil @@ -87,7 +87,7 @@ func (b *server) registerStandaloneTools(ctx context.Context) { } } - b.registerWebFetchTool(cfg) + b.registerWebFetchTool(ctx, cfg) if entry := sendfile.Tool(cfg.Chat.Workspace); entry != nil { if err := b.toolReg.Register(*entry); err != nil { @@ -99,7 +99,7 @@ func (b *server) registerStandaloneTools(ctx context.Context) { log.InfoContext(ctx, "file sending disabled (chat.workspace is unset)") } - b.registerMinimaxTool(cfg, log) + b.registerMinimaxTool(ctx, cfg, log) b.registerWorkflowTools(ctx) } @@ -143,7 +143,7 @@ func (s integrationSource) Policies(ctx context.Context) ([]access.Policy, error return s.store.ListPolicies(ctx) } -func (b *server) registerWebFetchTool(cfg config.Config) { +func (b *server) registerWebFetchTool(ctx context.Context, cfg config.Config) { log := b.log entry := webfetch.Tool(webfetch.Config{ Enabled: cfg.Tools.WebFetch.IsEnabled(), @@ -152,36 +152,36 @@ func (b *server) registerWebFetchTool(cfg config.Config) { AllowPrivateNetworks: cfg.Tools.WebFetch.AllowPrivateNetworks, }) if entry == nil { - log.Info("web fetch disabled") + log.InfoContext(ctx, "web fetch disabled") return } if err := b.toolReg.Register(*entry); err != nil { - log.Warn("web_fetch registration failed", "err", err) + log.WarnContext(ctx, "web_fetch registration failed", "err", err) return } - log.Info("web fetch enabled", + log.InfoContext(ctx, "web fetch enabled", "allow_private_networks", cfg.Tools.WebFetch.AllowPrivateNetworks) } -func (b *server) registerMinimaxTool(cfg config.Config, log *slog.Logger) { +func (b *server) registerMinimaxTool(ctx context.Context, cfg config.Config, log *slog.Logger) { if !cfg.Tools.Minimax.IsEnabled() { - log.Info("minimax video generation disabled") + log.InfoContext(ctx, "minimax video generation disabled") return } apiKey, err := b.secrets.Resolve(cfg.Tools.Minimax.APIKey) if err != nil { - log.Warn("minimax video generation enabled but the API key failed to resolve; tool not registered", "err", err) + log.WarnContext(ctx, "minimax video generation enabled but the API key failed to resolve; tool not registered", "err", err) return } if apiKey == "" { - log.Warn("minimax video generation enabled but no API key is configured; tool not registered") + log.WarnContext(ctx, "minimax video generation enabled but no API key is configured; tool not registered") return } provider, err := minimax.New(minimax.Config{APIKey: apiKey, BaseURL: cfg.Tools.Minimax.BaseURL}) if err != nil { - log.Warn("minimax video generation enabled but the provider could not be built; tool not registered", "err", err) + log.WarnContext(ctx, "minimax video generation enabled but the provider could not be built; tool not registered", "err", err) return } @@ -190,10 +190,10 @@ func (b *server) registerMinimaxTool(cfg config.Config, log *slog.Logger) { return } if err := b.toolReg.Register(*entry); err != nil { - log.Warn("generate_video registration failed", "err", err) + log.WarnContext(ctx, "generate_video registration failed", "err", err) return } - log.Info("minimax video generation enabled") + log.InfoContext(ctx, "minimax video generation enabled") } // toolLimits reads the configured per-turn result limits. diff --git a/internal/app/statestore/policy_guard.go b/internal/app/statestore/policy_guard.go index 27019264f..48017ef5c 100644 --- a/internal/app/statestore/policy_guard.go +++ b/internal/app/statestore/policy_guard.go @@ -92,7 +92,7 @@ func (g guardedPolicies) ownerCanManage(ctx context.Context, engine *accessengin } resource := access.Resource{Kind: access.KindPolicy, Org: orgID} for _, principal := range principals { - if engine.Authorize(principal, access.ActionManagePolicies, resource, access.Context{}).Allowed { + if engine.Authorize(ctx, principal, access.ActionManagePolicies, resource, access.Context{}).Allowed { return true, nil } } diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 12c51fefb..33542bc4e 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -753,7 +753,7 @@ func (d *Daemon) authorizeDispatch(ctx context.Context, b binding.Binding, c sto Kind: access.KindWorkflow, ID: b.Workflow, Org: principal.Org, Workspace: b.WorkspaceID, } contextValue := access.Context{Signature: signatureOf(c), Addr: c.RemoteAddr} - decision := d.Access.Authorize(principal, access.ActionRun, resource, contextValue) + decision := d.Access.Authorize(ctx, principal, access.ActionRun, resource, contextValue) if decision.Allowed { return true } diff --git a/internal/daemon/workflow_identity.go b/internal/daemon/workflow_identity.go index eefd04683..1b6b3e37c 100644 --- a/internal/daemon/workflow_identity.go +++ b/internal/daemon/workflow_identity.go @@ -124,7 +124,7 @@ func (d *Daemon) mayRunWorkflow(ctx context.Context, task *workflow.Task, id ide if resource.Org == "" { resource.Org = principal.Org } - decision := d.Access.Authorize(principal, access.ActionRun, resource, access.Context{Signature: task.BindingSignature, Addr: task.BindingAddr}) + decision := d.Access.Authorize(ctx, principal, access.ActionRun, resource, access.Context{Signature: task.BindingSignature, Addr: task.BindingAddr}) if decision.Allowed { return true, nil } diff --git a/internal/daemon/workflow_identity_test.go b/internal/daemon/workflow_identity_test.go index 7cefb2681..616f43552 100644 --- a/internal/daemon/workflow_identity_test.go +++ b/internal/daemon/workflow_identity_test.go @@ -66,7 +66,7 @@ func (agentDirectory) ListOrgAgents(_ context.Context, owner org.OrgID) ([]org.A // runAllowed permits run only to the identities it lists. type runAllowed map[identity.IdentityID]bool -func (r runAllowed) Authorize(p access.Principal, _ access.Action, _ access.Resource, _ access.Context) access.Decision { +func (r runAllowed) Authorize(_ context.Context, p access.Principal, _ access.Action, _ access.Resource, _ access.Context) access.Decision { if r[p.IdentityID] { return access.Allowed() } @@ -78,7 +78,7 @@ func (runAllowed) Reload(context.Context) error { return nil } // unavailableChain is a chain that cannot be read: nothing is evaluated. type unavailableChain struct{ runAllowed } -func (unavailableChain) Authorize(access.Principal, access.Action, access.Resource, access.Context) access.Decision { +func (unavailableChain) Authorize(context.Context, access.Principal, access.Action, access.Resource, access.Context) access.Decision { return access.Unavailable() } @@ -159,11 +159,11 @@ func TestWorkflowIdentity(t *testing.T) { // conditioned on context.signature does. type signedOnly struct{ runAllowed } -func (s signedOnly) Authorize(p access.Principal, a access.Action, r access.Resource, c access.Context) access.Decision { +func (s signedOnly) Authorize(ctx context.Context, p access.Principal, a access.Action, r access.Resource, c access.Context) access.Decision { if c.Signature != "valid" { return access.DeniedAt(access.LevelOrg, nil) } - return s.runAllowed.Authorize(p, a, r, c) + return s.runAllowed.Authorize(ctx, p, a, r, c) } // TestWorkflowRunCarriesEventContext pins that the run-time re-check sees the diff --git a/internal/domain/access/authorizer.go b/internal/domain/access/authorizer.go index e1110a3b5..4cfafee8f 100644 --- a/internal/domain/access/authorizer.go +++ b/internal/domain/access/authorizer.go @@ -4,6 +4,8 @@ package access import ( + "context" + "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/org" ) @@ -15,7 +17,7 @@ type Authorizer interface { // the request. A request is allowed only if every level with policies // for it permits it; a forbid at any level wins; with no permit the // request is denied. - Authorize(p Principal, a Action, r Resource, c Context) Decision + Authorize(ctx context.Context, p Principal, a Action, r Resource, c Context) Decision } // Validator checks a policy's Cedar syntax and entity vocabulary. @@ -29,5 +31,5 @@ const SenderID identity.IdentityID = "sender" // DeliveryAuthorizer decides whether an address may deliver an event to a // source. Only the capture receiver asks. type DeliveryAuthorizer interface { - AuthorizeDelivery(orgID org.OrgID, sourcePath, addr string) Decision + AuthorizeDelivery(ctx context.Context, orgID org.OrgID, sourcePath, addr string) Decision } diff --git a/internal/gateway/tool_access.go b/internal/gateway/tool_access.go index 97a965d2e..583af1430 100644 --- a/internal/gateway/tool_access.go +++ b/internal/gateway/tool_access.go @@ -27,7 +27,7 @@ func guarded(authz access.Authorizer, action access.Action, kind access.Resource if id != nil { resource.ID = id(input) } - decision := authz.Authorize(p, action, resource, access.Context{}) + decision := authz.Authorize(ctx, p, action, resource, access.Context{}) if decision.Allowed { return handler(ctx, input) } diff --git a/internal/gateway/tool_access_test.go b/internal/gateway/tool_access_test.go index 696695d8e..ddba7b4ae 100644 --- a/internal/gateway/tool_access_test.go +++ b/internal/gateway/tool_access_test.go @@ -10,7 +10,7 @@ import ( type allowOnly struct{ action access.Action } -func (a allowOnly) Authorize(_ access.Principal, action access.Action, _ access.Resource, _ access.Context) access.Decision { +func (a allowOnly) Authorize(_ context.Context, _ access.Principal, action access.Action, _ access.Resource, _ access.Context) access.Decision { return access.Decision{Allowed: action == a.action} } diff --git a/internal/infrastructure/access/delivery_test.go b/internal/infrastructure/access/delivery_test.go index 2c2cec7c8..f36d9323f 100644 --- a/internal/infrastructure/access/delivery_test.go +++ b/internal/infrastructure/access/delivery_test.go @@ -45,7 +45,7 @@ func TestAuthorizeDelivery(t *testing.T) { if acting == "" { acting = org.DefaultOrgID } - if got := engine.AuthorizeDelivery(acting, tt.source, tt.addr); got.Allowed != tt.allowed { + if got := engine.AuthorizeDelivery(t.Context(), acting, tt.source, tt.addr); got.Allowed != tt.allowed { t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got) } }) diff --git a/internal/infrastructure/access/engine.go b/internal/infrastructure/access/engine.go index 557c129d9..467a51c8b 100644 --- a/internal/infrastructure/access/engine.go +++ b/internal/infrastructure/access/engine.go @@ -4,6 +4,7 @@ package access import ( + "context" "fmt" "sync" @@ -192,7 +193,7 @@ func compile(p access.Policy) (*cedar.Policy, error) { } // Authorize evaluates the chain for one request. -func (e *Engine) Authorize(p access.Principal, a access.Action, r access.Resource, c access.Context) access.Decision { +func (e *Engine) Authorize(_ context.Context, p access.Principal, a access.Action, r access.Resource, c access.Context) access.Decision { if e.pending { return access.Unavailable() } @@ -215,7 +216,7 @@ func (e *Engine) Authorize(p access.Principal, a access.Action, r access.Resourc // the instance network rules, then the source's own object policies. A // sender has no identity or role, so the org and workspace role policies // never apply to it. -func (e *Engine) AuthorizeDelivery(orgID org.OrgID, sourcePath, addr string) access.Decision { +func (e *Engine) AuthorizeDelivery(_ context.Context, orgID org.OrgID, sourcePath, addr string) access.Decision { if e.pending { return access.Unavailable() } diff --git a/internal/infrastructure/access/engine_test.go b/internal/infrastructure/access/engine_test.go index 47fecda4b..90ca028bf 100644 --- a/internal/infrastructure/access/engine_test.go +++ b/internal/infrastructure/access/engine_test.go @@ -38,7 +38,7 @@ func TestInvalidInstancePolicy(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := engine.Authorize(tt.principal, tt.action, access.Resource{Kind: tt.kind}, access.Context{}) + got := engine.Authorize(t.Context(), tt.principal, tt.action, access.Resource{Kind: tt.kind}, access.Context{}) if got.Allowed != tt.allowed { t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got) } @@ -67,7 +67,7 @@ func TestReadAuditRoleBoundary(t *testing.T) { IdentityID: "person", Org: auditOrg, Memberships: []org.Membership{{IdentityID: "person", OrgID: auditOrg, Role: tt.role}}, } - got := engine.Authorize(p, access.ActionReadAudit, access.Resource{Kind: access.KindAudit, Org: auditOrg}, access.Context{}) + got := engine.Authorize(t.Context(), p, access.ActionReadAudit, access.Resource{Kind: access.KindAudit, Org: auditOrg}, access.Context{}) if got.Allowed != tt.allowed { t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got) } @@ -106,7 +106,7 @@ func TestWorkspaceEnvironment(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource := access.Resource{Kind: access.KindWorkflow, ID: "triage", Org: "acme", Workspace: tt.workspace} - if got := engine.Authorize(dev, tt.action, resource, access.Context{}); got.Allowed != tt.allowed { + if got := engine.Authorize(t.Context(), dev, tt.action, resource, access.Context{}); got.Allowed != tt.allowed { t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got) } }) diff --git a/internal/infrastructure/access/live.go b/internal/infrastructure/access/live.go index cfe695299..f5101c7a6 100644 --- a/internal/infrastructure/access/live.go +++ b/internal/infrastructure/access/live.go @@ -11,10 +11,9 @@ import ( "github.com/samcharles93/archie-core/internal/domain/org" ) -// 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. +// versionTimeout bounds one check of the version and the policies. Without a +// deadline 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 @@ -23,10 +22,6 @@ const versionTimeout = 5 * time.Second // holds may predate the edit, and standing on it would be exactly the allow // the edit meant to remove. type Live struct { - // 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 @@ -46,9 +41,8 @@ type Live struct { // 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 { +func NewPending(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)) @@ -84,8 +78,8 @@ func (l *Live) Reload(ctx context.Context) error { // 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) +func (l *Live) current(ctx context.Context) *Engine { + ctx, cancel := context.WithTimeout(ctx, versionTimeout) defer cancel() // The version before the list, never after and never in the same @@ -95,7 +89,7 @@ func (l *Live) current() *Engine { // 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) + l.log.WarnContext(ctx, "access policy version unavailable; refusing until it is readable", "err", err) return pendingEngine } if version == l.stamp.Load() { @@ -108,7 +102,7 @@ func (l *Live) current() *Engine { 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) + l.log.WarnContext(ctx, "access policies not reloaded; refusing until they load", "err", err) return pendingEngine } return l.engine.Load() @@ -133,12 +127,15 @@ func (l *Live) apply(ctx context.Context, version int64) error { return nil } -func (l *Live) Authorize(p access.Principal, a access.Action, r access.Resource, c access.Context) access.Decision { - return l.current().Authorize(p, a, r, c) +func (l *Live) Authorize(ctx context.Context, p access.Principal, a access.Action, r access.Resource, c access.Context) access.Decision { + return l.current(ctx).Authorize(ctx, p, a, r, c) } -func (l *Live) AuthorizeDelivery(orgID org.OrgID, sourcePath, addr string) access.Decision { - return l.current().AuthorizeDelivery(orgID, sourcePath, addr) +// AuthorizeDelivery has no caller context: the DeliveryAuthorizer contract +// carries none, so its check runs under the background context and the +// version deadline bounds it. +func (l *Live) AuthorizeDelivery(ctx context.Context, orgID org.OrgID, sourcePath, addr string) access.Decision { + return l.current(ctx).AuthorizeDelivery(ctx, orgID, sourcePath, addr) } // Problems reports the current engine's invalid policies. @@ -147,7 +144,7 @@ func (l *Live) Problems() []Problem { return l.engine.Load().Problems() } // 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) + live := NewPending(version, list, log) if err := live.Reload(ctx); err != nil { log.WarnContext(ctx, "access policies not loaded; requests are refused until they load", "err", err) } diff --git a/internal/infrastructure/access/live_test.go b/internal/infrastructure/access/live_test.go index 4eaef8b09..16740ab02 100644 --- a/internal/infrastructure/access/live_test.go +++ b/internal/infrastructure/access/live_test.go @@ -13,7 +13,7 @@ import ( // is up refuses every request until the first load succeeds, then allows. func TestPendingChainRefusesUntilLoaded(t *testing.T) { loaded := false - live := NewPending(t.Context(), func(context.Context) (int64, error) { + live := NewPending(func(context.Context) (int64, error) { return 1, nil }, func(context.Context) ([]access.Policy, error) { if !loaded { @@ -23,7 +23,7 @@ func TestPendingChainRefusesUntilLoaded(t *testing.T) { }, slog.New(slog.DiscardHandler)) request := func() access.Decision { - return live.Authorize(access.SharedTokenOwner(), access.ActionRead, + return live.Authorize(t.Context(), access.SharedTokenOwner(), access.ActionRead, access.Resource{Kind: access.KindDashboard}, access.Context{}) } @@ -36,7 +36,7 @@ func TestPendingChainRefusesUntilLoaded(t *testing.T) { if got := live.Problems(); got != nil { t.Fatalf("Problems before load = %v, want nil", got) } - if decision := live.AuthorizeDelivery("acme", "sources/x", "10.0.0.1"); decision.Allowed || + if decision := live.AuthorizeDelivery(t.Context(), "acme", "sources/x", "10.0.0.1"); decision.Allowed || !errors.Is(decision.Err, access.ErrChainUnavailable) { t.Fatalf("delivery before load = %+v, want the not-loaded refusal", decision) } @@ -86,7 +86,7 @@ func TestLiveAppliesAPolicyEditBeforeTheNextDecision(t *testing.T) { 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, + return live.Authorize(t.Context(), access.SharedTokenOwner(), access.ActionRead, access.Resource{Kind: access.KindDashboard}, access.Context{}) } tests := []struct { @@ -124,7 +124,7 @@ func TestLiveAppliesAPolicyEditBeforeTheNextDecision(t *testing.T) { 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)) + live := NewPending(store.PoliciesVersion, store.ListPolicies, slog.New(slog.DiscardHandler)) if err := live.Reload(t.Context()); err != nil { t.Fatal(err) } diff --git a/internal/infrastructure/captureintake/org_test.go b/internal/infrastructure/captureintake/org_test.go index ca75f4ec8..b918e1dec 100644 --- a/internal/infrastructure/captureintake/org_test.go +++ b/internal/infrastructure/captureintake/org_test.go @@ -40,7 +40,7 @@ type stubDelivery struct { decision access.Decision } -func (s *stubDelivery) AuthorizeDelivery(orgID org.OrgID, _, _ string) access.Decision { +func (s *stubDelivery) AuthorizeDelivery(_ context.Context, orgID org.OrgID, _, _ string) access.Decision { s.orgID = orgID return s.decision } diff --git a/internal/infrastructure/captureintake/receiver.go b/internal/infrastructure/captureintake/receiver.go index 1e72db0b6..a898e5746 100644 --- a/internal/infrastructure/captureintake/receiver.go +++ b/internal/infrastructure/captureintake/receiver.go @@ -175,7 +175,7 @@ func (rc *Receiver) refusal(r *http.Request, path string, owner org.OrgID) int { return 0 } addr := remoteAddrHost(r) - decision := rc.Delivery.AuthorizeDelivery(owner, path, addr) + decision := rc.Delivery.AuthorizeDelivery(r.Context(), owner, path, addr) if decision.Allowed { return 0 } diff --git a/internal/infrastructure/readiness/probes_test.go b/internal/infrastructure/readiness/probes_test.go index 6b7cec56a..756d3f206 100644 --- a/internal/infrastructure/readiness/probes_test.go +++ b/internal/infrastructure/readiness/probes_test.go @@ -16,7 +16,7 @@ import ( // once they do, without rebuilding the probe. func TestProblemProbeFollowsTheChain(t *testing.T) { up := false - chain := infraaccess.NewPending(t.Context(), func(context.Context) (int64, error) { + chain := infraaccess.NewPending(func(context.Context) (int64, error) { return 1, nil }, func(context.Context) ([]access.Policy, error) { if !up { diff --git a/internal/infrastructure/staterpc/accounts.go b/internal/infrastructure/staterpc/accounts.go index 10361e10d..fd95b7222 100644 --- a/internal/infrastructure/staterpc/accounts.go +++ b/internal/infrastructure/staterpc/accounts.go @@ -162,7 +162,7 @@ func (s *server) authorizeAccountManagement(ctx context.Context) error { if err != nil { return status.Error(codes.Unavailable, "access policies unavailable") } - decision := infraaccess.New(policies).Authorize(p, access.ActionManageMembers, access.Resource{Kind: access.KindMember, Org: p.Org}, access.Context{}) + decision := infraaccess.New(policies).Authorize(ctx, p, access.ActionManageMembers, access.Resource{Kind: access.KindMember, Org: p.Org}, access.Context{}) if !decision.Allowed { return status.Error(codes.PermissionDenied, "account management refused") } diff --git a/internal/webui/authorize.go b/internal/webui/authorize.go index 74d0cc116..f813aa4de 100644 --- a/internal/webui/authorize.go +++ b/internal/webui/authorize.go @@ -54,7 +54,7 @@ func (s *Server) authorize(h http.Handler) http.Handler { return } peer, _, _ := net.SplitHostPort(r.RemoteAddr) - decision := s.Access.Authorize(principal, action, resource, access.Context{Addr: peer}) + decision := s.Access.Authorize(ctx, principal, action, resource, access.Context{Addr: peer}) if !decision.Allowed { if errors.Is(decision.Err, access.ErrChainUnavailable) { // Nothing was evaluated: the chain has not loaded. This is an @@ -87,7 +87,7 @@ func (s *Server) authorizeCreateIn(w http.ResponseWriter, r *http.Request, kind return true } resource := access.Resource{Kind: kind, Org: principal.Org, Workspace: workspace} - decision := s.Access.Authorize(principal, access.ActionCreate, resource, access.Context{}) + decision := s.Access.Authorize(r.Context(), principal, access.ActionCreate, resource, access.Context{}) if decision.Allowed { return true } diff --git a/internal/webui/authorize_test.go b/internal/webui/authorize_test.go index 2da5fb353..7c3673e63 100644 --- a/internal/webui/authorize_test.go +++ b/internal/webui/authorize_test.go @@ -46,7 +46,7 @@ func (s operatorPrincipals) PrincipalFor(_ context.Context, id identity.Identity type allowAll struct{} -func (allowAll) Authorize(access.Principal, access.Action, access.Resource, access.Context) access.Decision { +func (allowAll) Authorize(context.Context, access.Principal, access.Action, access.Resource, access.Context) access.Decision { return access.Allowed() } @@ -56,7 +56,7 @@ func (allowAll) Ready() bool { return true } // is the not-loaded refusal, which no policy evaluated. type notReadyChain struct{} -func (notReadyChain) Authorize(access.Principal, access.Action, access.Resource, access.Context) access.Decision { +func (notReadyChain) Authorize(context.Context, access.Principal, access.Action, access.Resource, access.Context) access.Decision { return access.Unavailable() } diff --git a/internal/webui/orgs_test.go b/internal/webui/orgs_test.go index 655873aa4..1d483937d 100644 --- a/internal/webui/orgs_test.go +++ b/internal/webui/orgs_test.go @@ -513,7 +513,7 @@ func TestOrgSelection(t *testing.T) { // recordingChain allows everything and keeps the last principal it judged. type recordingChain struct{ last access.Principal } -func (c *recordingChain) Authorize(p access.Principal, _ access.Action, _ access.Resource, _ access.Context) access.Decision { +func (c *recordingChain) Authorize(_ context.Context, p access.Principal, _ access.Action, _ access.Resource, _ access.Context) access.Decision { c.last = p return access.Allowed() } diff --git a/internal/webui/task_recovery.go b/internal/webui/task_recovery.go index 5a6c7f020..0e40f49d2 100644 --- a/internal/webui/task_recovery.go +++ b/internal/webui/task_recovery.go @@ -84,5 +84,5 @@ func (s *Server) mayCall(r *http.Request, method, path string) bool { } action, kind, id := accessRequest(&http.Request{Method: method, URL: &url.URL{Path: path}}) resource, err := s.recordResource(r.Context(), kind, id) - return err == nil && s.Access.Authorize(principal, action, resource, access.Context{}).Allowed + return err == nil && s.Access.Authorize(r.Context(), principal, action, resource, access.Context{}).Allowed }