Files
archie/internal/gateway/integration_tools.go
T
sam aadeac9221 feat(gateway): read instructions and access policies from the workflow tools
settings_get now reads three more kinds:
- soul, the instruction text layered into a workflow run;
- task-policy, the diff cap and alert delivery settings;
- org-model-policy, which models an org may use.

None of them holds a secret value: task-policy's notify_secret and the
secrets elsewhere in the readable kinds are SecretRef references (engine and
key), never the resolved value.

access_policies_list is new: the caller org's Cedar policies (id, level,
scope, text), and the instance policies when the caller owns the instance,
which is what the dashboard lists for the same read. It is guarded as
access.ActionRead on access.KindPolicy, the check the dashboard makes.
IntegrationSource gained Policies, implemented over the State Store's
ListPolicies.

workflow_get now says how its instructions field layers: after the
instance's and the organisation's, before the repository's.
2026-10-18 02:39:25 +11:00

126 lines
5.1 KiB
Go

package gateway
import (
"context"
"encoding/json"
"fmt"
"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"
)
// IntegrationSource is what the integration tools read, in the caller's org.
type IntegrationSource interface {
Resource(ctx context.Context, kind string) (json.RawMessage, error)
EventTypes(ctx context.Context) ([]eventtype.EventType, error)
Bindings(ctx context.Context) ([]binding.Binding, error)
Policies(ctx context.Context) ([]access.Policy, error)
}
// readableKinds are the settings that shape what a workflow can do, what starts
// it and what it is told to do. Provider, channel and connection settings are
// left out: they carry endpoints and secret references an agent has no use for.
// soul is the instructions text, task-policy and org-model-policy carry limits
// and settings whose secrets are references, never values.
var readableKinds = []string{
"workflow-execution-settings", "workflow-enablement", "agent-profiles", "skills",
"schedules", "eda-playbooks", "credential-bindings", "identity-grants",
"soul", "task-policy", "org-model-policy",
}
// visiblePolicies keeps what the caller's dashboard shows: the caller org's
// policies, and the instance policies when the caller owns the instance.
func visiblePolicies(ctx context.Context, policies []access.Policy) []access.Policy {
principal, ok := access.PrincipalFromContext(ctx)
if !ok {
return nil
}
out := make([]access.Policy, 0, len(policies))
for _, policy := range policies {
instance := policy.Level == access.LevelInstance
if (instance && principal.InstanceAdmin()) || (!instance && policy.OrgID == principal.Org) {
out = append(out, policy)
}
}
return out
}
// IntegrationTools returns read-only tools for investigating what starts
// workflows and how they can be customised. Nil source registers none.
// 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, ", ") + ".",
Classification: tools.ClassIdempotent,
Schema: tools.JSONSchema{
"type": "object",
"properties": map[string]any{"kind": map[string]any{"type": "string", "enum": readableKinds}},
"required": []any{"kind"},
},
Handler: func(ctx context.Context, input map[string]any) (any, error) {
kind := asString(input["kind"])
if !slices.Contains(readableKinds, kind) {
return nil, fmt.Errorf("settings_get: %q is not readable; use one of %s", kind, strings.Join(readableKinds, ", "))
}
value, err := source.Resource(ctx, kind)
if err != nil {
return nil, fmt.Errorf("settings_get: %w", err)
}
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.",
Classification: tools.ClassIdempotent,
Schema: tools.JSONSchema{"type": "object", "properties": map[string]any{}},
Handler: func(ctx context.Context, _ map[string]any) (any, error) {
types, err := source.EventTypes(ctx)
if err != nil {
return nil, fmt.Errorf("event_types_list: %w", err)
}
return types, nil
},
}),
guarded(authz, access.ActionRead, access.KindPolicy, nil, tools.ToolEntry{
Name: "access_policies_list",
Toolset: "workflows",
Description: "List the Cedar access policies that decide what a workflow and its caller may do: each one's id, level, scope and policy text.",
Classification: tools.ClassIdempotent,
Schema: tools.JSONSchema{"type": "object", "properties": map[string]any{}},
Handler: func(ctx context.Context, _ map[string]any) (any, error) {
policies, err := source.Policies(ctx)
if err != nil {
return nil, fmt.Errorf("access_policies_list: %w", err)
}
return visiblePolicies(ctx, policies), 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.",
Classification: tools.ClassIdempotent,
Schema: tools.JSONSchema{"type": "object", "properties": map[string]any{}},
Handler: func(ctx context.Context, _ map[string]any) (any, error) {
bindings, err := source.Bindings(ctx)
if err != nil {
return nil, fmt.Errorf("bindings_list: %w", err)
}
return bindings, nil
},
}),
}
}