fix(gateway): workflow and integration tools ask the access policies as their caller
This commit is contained in:
1 parent
39c3c47ea2
commit
7768fa808d
8 files changed
+132
-29
No files matched your search
@@ -54,7 +54,7 @@ func (b *server) startGatewayRuntime(ctx context.Context, actor gateway.ChatTask
|
||||
if err := b.registerTools(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b.registerStandaloneTools()
|
||||
b.registerStandaloneTools(ctx)
|
||||
b.serveSkills(ctx)
|
||||
b.addCleanup(shutdownCapabilityHost(ctx, b.capabilityHost, b.log))
|
||||
if err := b.capabilityHost.Start(ctx); err != nil {
|
||||
|
||||
@@ -11,9 +11,11 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"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"
|
||||
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/taskmcp"
|
||||
@@ -66,7 +68,7 @@ func (b *server) registerTools() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *server) registerStandaloneTools() {
|
||||
func (b *server) registerStandaloneTools(ctx context.Context) {
|
||||
cfg, log := b.cfg, b.log
|
||||
|
||||
if spillDir := cfg.Tools.Policy.SpillDir; spillDir != "" {
|
||||
@@ -91,15 +93,16 @@ func (b *server) registerStandaloneTools() {
|
||||
}
|
||||
|
||||
b.registerMinimaxTool(cfg, log)
|
||||
b.registerWorkflowTools()
|
||||
b.registerWorkflowTools(ctx)
|
||||
}
|
||||
|
||||
// registerWorkflowTools puts the workflow and integration tools in the shared
|
||||
// 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() {
|
||||
func (b *server) registerWorkflowTools(ctx context.Context) {
|
||||
authz := infraaccess.StartLive(ctx, b.stateStore.ListPolicies, applystatus.RestampInterval, b.log)
|
||||
workflows := controlplanerpc.NewWorkflows(b.stateStore.ControlPlane())
|
||||
entries := append(gateway.WorkflowTools(workflows), gateway.IntegrationTools(integrationSource{Workflows: workflows, store: b.stateStore})...)
|
||||
entries := append(gateway.WorkflowTools(workflows, authz), gateway.IntegrationTools(integrationSource{Workflows: workflows, store: b.stateStore}, authz)...)
|
||||
for _, entry := range entries {
|
||||
if err := b.toolReg.Register(entry); err != nil {
|
||||
b.log.Warn("workflow tool registration failed", "tool", entry.Name, "err", err)
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/presence"
|
||||
@@ -33,14 +32,7 @@ import (
|
||||
// retrying, 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 {
|
||||
// The chain decides for every org, so it loads as the dashboard service,
|
||||
// never as the person whose policy edit triggered a reload.
|
||||
load := func(ctx context.Context) ([]access.Policy, error) {
|
||||
return store.ListPolicies(access.WithoutPrincipal(ctx))
|
||||
}
|
||||
live := infraaccess.NewPending(load, log)
|
||||
go live.Run(ctx, applystatus.RestampInterval)
|
||||
return live
|
||||
return infraaccess.StartLive(ctx, store.ListPolicies, applystatus.RestampInterval, log)
|
||||
}
|
||||
|
||||
// Run serves the dashboard until ctx is cancelled. It resolves its own
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eventtype"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
@@ -29,12 +30,13 @@ var readableKinds = []string{
|
||||
|
||||
// IntegrationTools returns read-only tools for investigating what starts
|
||||
// workflows and how they can be customised. Nil source registers none.
|
||||
func IntegrationTools(source IntegrationSource) []tools.ToolEntry {
|
||||
if source == nil {
|
||||
// Each is checked as the dashboard checks the same read.
|
||||
func IntegrationTools(source IntegrationSource, authz access.Authorizer) []tools.ToolEntry {
|
||||
if source == nil || authz == nil {
|
||||
return nil
|
||||
}
|
||||
return []tools.ToolEntry{
|
||||
{
|
||||
guarded(authz, access.ActionRead, access.KindPolicy, inputID("kind"), tools.ToolEntry{
|
||||
Name: "settings_get",
|
||||
Toolset: "workflows",
|
||||
Description: "Return one settings document as JSON. Kinds: " + strings.Join(readableKinds, ", ") + ".",
|
||||
@@ -55,8 +57,8 @@ func IntegrationTools(source IntegrationSource) []tools.ToolEntry {
|
||||
}
|
||||
return value, nil
|
||||
},
|
||||
},
|
||||
{
|
||||
}),
|
||||
guarded(authz, access.ActionRead, access.KindEventType, nil, tools.ToolEntry{
|
||||
Name: "event_types_list",
|
||||
Toolset: "workflows",
|
||||
Description: "List the named event types: each one's source, the rule that recognises it and the fields its payload carries.",
|
||||
@@ -69,8 +71,8 @@ func IntegrationTools(source IntegrationSource) []tools.ToolEntry {
|
||||
}
|
||||
return types, nil
|
||||
},
|
||||
},
|
||||
{
|
||||
}),
|
||||
guarded(authz, access.ActionRead, access.KindBinding, nil, tools.ToolEntry{
|
||||
Name: "bindings_list",
|
||||
Toolset: "workflows",
|
||||
Description: "List the bindings that start workflows from events: what each matches, its filter and the workflow it runs.",
|
||||
@@ -83,6 +85,6 @@ func IntegrationTools(source IntegrationSource) []tools.ToolEntry {
|
||||
}
|
||||
return bindings, nil
|
||||
},
|
||||
},
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
// guarded makes entry ask the access policies before it runs, as the caller
|
||||
// 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.
|
||||
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) {
|
||||
p, ok := access.PrincipalFromContext(ctx)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s: no caller to authorize", entry.Name)
|
||||
}
|
||||
resource := access.Resource{Kind: kind}
|
||||
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)
|
||||
}
|
||||
return handler(ctx, input)
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
// fixedID names the same record for every call.
|
||||
func fixedID(id string) func(map[string]any) string {
|
||||
return func(map[string]any) string { return id }
|
||||
}
|
||||
|
||||
// inputID names the record the call's input names.
|
||||
func inputID(key string) func(map[string]any) string {
|
||||
return func(input map[string]any) string { return asString(input[key]) }
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
type allowOnly struct{ action access.Action }
|
||||
|
||||
func (a allowOnly) Authorize(_ access.Principal, action access.Action, _ access.Resource, _ access.Context) access.Decision {
|
||||
return access.Decision{Allowed: action == a.action}
|
||||
}
|
||||
|
||||
func TestGuardedToolsRunOnlyForAnAllowedCaller(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
caller bool
|
||||
allow access.Action
|
||||
action access.Action
|
||||
runs bool
|
||||
}{
|
||||
{"allowed caller", true, access.ActionRead, access.ActionRead, true},
|
||||
{"denied caller", true, access.ActionRead, access.ActionManageIdentities, false},
|
||||
{"no caller", false, access.ActionRead, access.ActionRead, false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ran := false
|
||||
entry := guarded(allowOnly{tc.allow}, tc.action, access.KindPolicy, nil, tools.ToolEntry{
|
||||
Name: "probe",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { ran = true; return nil, nil },
|
||||
})
|
||||
ctx := context.Background()
|
||||
if tc.caller {
|
||||
ctx = access.WithPrincipal(ctx, access.Principal{IdentityID: "operator"})
|
||||
}
|
||||
_, err := entry.Handler(ctx, nil)
|
||||
if ran != tc.runs || (err == nil) != tc.runs {
|
||||
t.Fatalf("ran=%v err=%v, want ran=%v", ran, err, tc.runs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
@@ -40,16 +41,21 @@ type WorkflowValidation struct {
|
||||
// WorkflowTools returns the workflow tools over source. The model writes only
|
||||
// drafts; publishing is left to the operator in the workflow editor. Nil
|
||||
// source registers none.
|
||||
func WorkflowTools(source WorkflowSource) []tools.ToolEntry {
|
||||
if source == nil {
|
||||
// Reads are checked as reading the workflow-definitions settings and a
|
||||
// draft as writing workflow-drafts, the checks the workflow editor gets.
|
||||
func WorkflowTools(source WorkflowSource, authz access.Authorizer) []tools.ToolEntry {
|
||||
if source == nil || authz == nil {
|
||||
return nil
|
||||
}
|
||||
read := func(entry tools.ToolEntry) tools.ToolEntry {
|
||||
return guarded(authz, access.ActionRead, access.KindPolicy, fixedID("workflow-definitions"), entry)
|
||||
}
|
||||
return []tools.ToolEntry{
|
||||
workflowListTool(source),
|
||||
workflowGetTool(source),
|
||||
workflowStepTypesTool(source),
|
||||
workflowValidateTool(source),
|
||||
workflowDraftTool(source),
|
||||
read(workflowListTool(source)),
|
||||
read(workflowGetTool(source)),
|
||||
read(workflowStepTypesTool(source)),
|
||||
read(workflowValidateTool(source)),
|
||||
guarded(authz, access.ActionManageIdentities, access.KindPolicy, fixedID("workflow-drafts"), workflowDraftTool(source)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -83,3 +83,14 @@ func (l *Live) AuthorizeDelivery(orgID org.OrgID, sourcePath, addr string) acces
|
||||
|
||||
// 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)
|
||||
return live
|
||||
}
|
||||
Reference in new issue
Block a user