282 lines
12 KiB
Go
282 lines
12 KiB
Go
package archiegateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/grpc/test/bufconn"
|
|
|
|
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/storepkg"
|
|
"github.com/samcharles93/archie-core/internal/domain/usage"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/tasktools"
|
|
"github.com/samcharles93/archie-core/internal/tools"
|
|
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
|
)
|
|
|
|
type taskToolGrants struct {
|
|
taskToolAuthority
|
|
task task.Task
|
|
principal access.Principal
|
|
accepted *storepkg.Authority
|
|
revoked bool
|
|
recorded []usage.Record
|
|
}
|
|
|
|
func (a *taskToolGrants) TaskPrincipalForCredential(context.Context, string) (*task.Task, identity.IdentityID, error) {
|
|
if a.revoked {
|
|
return nil, "", errors.New("revoked")
|
|
}
|
|
return &a.task, a.principal.IdentityID, nil
|
|
}
|
|
|
|
func (a *taskToolGrants) PrincipalFor(context.Context, identity.IdentityID) (access.Principal, error) {
|
|
return a.principal, nil
|
|
}
|
|
|
|
func (a *taskToolGrants) RecordUsage(_ context.Context, value usage.Record) error {
|
|
a.recorded = append(a.recorded, value)
|
|
return nil
|
|
}
|
|
|
|
func (a *taskToolGrants) GetInstalled(context.Context, string) (storepkg.Installed, error) {
|
|
return storepkg.Installed{AcceptedAuthority: a.accepted}, nil
|
|
}
|
|
|
|
func TestTaskToolAuthorization(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name, caller, token, tool string
|
|
profile []string
|
|
packaged, revoked bool
|
|
accepted *storepkg.Authority
|
|
principalOrg org.OrgID
|
|
status string
|
|
want codes.Code
|
|
}{
|
|
{name: "allowed", tool: "fetch", want: codes.OK},
|
|
{name: "profile denial", tool: "fetch", profile: []string{"other"}, want: codes.PermissionDenied},
|
|
{name: "unaccepted package", tool: "fetch", packaged: true, want: codes.PermissionDenied},
|
|
{name: "accepted package", tool: "fetch", packaged: true, accepted: &storepkg.Authority{Tools: []string{"fetch"}}},
|
|
{name: "package intersection", tool: "fetch", packaged: true, accepted: &storepkg.Authority{Tools: []string{"fetch"}}, profile: []string{"other"}, want: codes.PermissionDenied},
|
|
{name: "workspace never served", tool: "shell", want: codes.PermissionDenied},
|
|
{name: "chat files never served", tool: "send_file", want: codes.PermissionDenied},
|
|
{name: "approval unavailable", tool: "danger", want: codes.PermissionDenied},
|
|
{name: "revoked", tool: "fetch", revoked: true, want: codes.Unauthenticated},
|
|
{name: "not running", tool: "fetch", status: "completed", want: codes.PermissionDenied},
|
|
{name: "other org principal", tool: "fetch", principalOrg: "victim", want: codes.PermissionDenied},
|
|
{name: "non daemon", caller: "archie-ui", tool: "fetch", want: codes.PermissionDenied},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
authority := &taskToolGrants{task: task.Task{ID: 7, Org: "acme", Status: "running", WorkflowDefinitionYAML: "id: work\nprofile: kit\nidentity: bot\n"}, principal: access.Principal{IdentityID: "bot", Org: "acme"}, accepted: tt.accepted, revoked: tt.revoked}
|
|
if tt.packaged {
|
|
authority.task.WorkflowDefinitionYAML += "package: sample\n"
|
|
}
|
|
if tt.status != "" {
|
|
authority.task.Status = tt.status
|
|
}
|
|
if tt.principalOrg != "" {
|
|
authority.principal.Org = tt.principalOrg
|
|
}
|
|
called := 0
|
|
reg := tools.NewRegistry()
|
|
for _, entry := range []tools.ToolEntry{
|
|
{Name: "fetch", Toolset: "web"}, {Name: "shell", Toolset: "workspace"}, {Name: "send_file", Toolset: "media"}, {Name: "danger", Classification: tools.RequiresApproval},
|
|
} {
|
|
entry.Schema = tools.JSONSchema{"type": "object"}
|
|
entry.Handler = func(ctx context.Context, _ map[string]any) (any, error) {
|
|
called++
|
|
p, _ := access.PrincipalFromContext(ctx)
|
|
if p.IdentityID != "bot" || org.OrgFromContext(ctx) != "acme" {
|
|
t.Fatal("tool executed with caller-selected authority")
|
|
}
|
|
return "result", nil
|
|
}
|
|
if err := reg.Register(entry); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
cfg := config.Config{Containers: config.ContainerConfig{Profiles: map[string]config.AgentProfile{"kit": {Kit: "image@sha256:pinned", Tools: tt.profile}}}}
|
|
s := &taskToolsServer{gateway: &server{cfgHolder: config.NewHolder(cfg), toolReg: reg, log: slog.New(slog.DiscardHandler)}, authority: authority, configFor: func(context.Context) (config.Config, error) { return cfg, nil }, sessions: make(map[[32]byte]*taskToolSession)}
|
|
caller := tt.caller
|
|
if caller == "" {
|
|
caller = "archied"
|
|
}
|
|
ctx := access.WithSource(t.Context(), caller)
|
|
result, err := s.ListTaskTools(ctx, &pb.ListTaskToolsRequest{RunCredential: "grant"})
|
|
if err == nil {
|
|
var listed []*mcp.Tool
|
|
if err := json.Unmarshal(result.ToolsJson, &listed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tool := range listed {
|
|
if slices.Contains([]string{"shell", "send_file", "danger"}, tool.Name) {
|
|
t.Fatalf("unsafe tool listed: %s", tool.Name)
|
|
}
|
|
}
|
|
}
|
|
_, err = s.CallTaskTool(ctx, &pb.CallTaskToolRequest{RunCredential: "grant", Name: tt.tool, ArgumentsJson: []byte(`{}`)})
|
|
if status.Code(err) != tt.want {
|
|
t.Fatalf("error %v, want %v", err, tt.want)
|
|
}
|
|
if tt.want == codes.OK && called != 1 {
|
|
t.Fatalf("calls %d, want 1", called)
|
|
}
|
|
if tt.want != codes.OK && called != 0 {
|
|
t.Fatal("denied request executed")
|
|
}
|
|
// Authorization must be checked again after a successful discovery.
|
|
authority.revoked = true
|
|
if _, err := s.CallTaskTool(ctx, &pb.CallTaskToolRequest{RunCredential: "grant", Name: "fetch", ArgumentsJson: []byte(`{}`)}); err == nil {
|
|
t.Fatal("revoked grant remained usable")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTaskToolGatewayThroughProxy(t *testing.T) {
|
|
authority := &taskToolGrants{task: task.Task{ID: 7, Org: "acme", Status: "running", WorkflowDefinitionYAML: "id: work\nprofile: kit\nidentity: bot\n"}, principal: access.Principal{IdentityID: "bot", Org: "acme"}}
|
|
reg := tools.NewRegistry()
|
|
calls := 0
|
|
blocked := make(chan struct{})
|
|
if err := reg.Register(tools.ToolEntry{Name: "echo", Toolset: "web", Schema: tools.JSONSchema{"type": "object", "properties": map[string]any{"value": map[string]any{"type": "string"}}, "required": []string{"value"}}, Handler: func(ctx context.Context, args map[string]any) (any, error) {
|
|
calls++
|
|
if org.OrgFromContext(ctx) != "acme" {
|
|
t.Error("lost task org")
|
|
}
|
|
if args["value"] == "block" {
|
|
close(blocked)
|
|
<-ctx.Done()
|
|
return nil, ctx.Err()
|
|
}
|
|
return args["value"], nil
|
|
}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg := config.Config{Containers: config.ContainerConfig{Profiles: map[string]config.AgentProfile{"kit": {Kit: "pinned", Tools: []string{"echo"}}}}}
|
|
service := &taskToolsServer{gateway: &server{cfgHolder: config.NewHolder(cfg), toolReg: reg, log: slog.New(slog.DiscardHandler)}, authority: authority, configFor: func(context.Context) (config.Config, error) { return cfg, nil }, sessions: make(map[[32]byte]*taskToolSession)}
|
|
defer service.Close(t.Context())
|
|
listener := bufconn.Listen(1 << 20)
|
|
rpc := grpc.NewServer(grpc.ChainUnaryInterceptor((rpcidentity.Callers{}).Unary()))
|
|
pb.RegisterTaskToolsServiceServer(rpc, service)
|
|
go func() { _ = rpc.Serve(listener) }()
|
|
defer rpc.Stop()
|
|
conn, err := grpc.NewClient("passthrough:///gateway", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) { return listener.Dial() }), grpc.WithUnaryInterceptor(staterpc.UnaryClientCredentialInterceptor("archied", "")))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
proxy := egress.NewProxy(nil, egress.ProxyOptions{TaskTools: tasktools.NewClient(conn)})
|
|
session, err := proxy.Register(egress.SessionOptions{Token: "proxy-password", RunCredential: "grant", Org: "acme"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer proxy.Revoke(t.Context(), session.Token())
|
|
session.EnterRuntime()
|
|
httpServer := httptest.NewServer(proxy)
|
|
defer httpServer.Close()
|
|
proxyURL, err := url.Parse(httpServer.URL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
proxyURL.User = url.UserPassword("archie", session.Token())
|
|
transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}
|
|
defer transport.CloseIdleConnections()
|
|
client := mcp.NewClient(&mcp.Implementation{Name: "test-harness", Version: "1"}, nil)
|
|
clientSession, err := client.Connect(t.Context(), &mcp.StreamableClientTransport{Endpoint: tasktools.Endpoint, HTTPClient: &http.Client{Transport: taskSentinelTransport{base: transport}}, DisableStandaloneSSE: true, MaxRetries: -1}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer clientSession.Close()
|
|
listed, err := clientSession.ListTools(t.Context(), nil)
|
|
if err != nil || len(listed.Tools) != 1 || listed.Tools[0].Name != "echo" {
|
|
t.Fatalf("discovery %v %v", listed, err)
|
|
}
|
|
result, err := clientSession.CallTool(t.Context(), &mcp.CallToolParams{Name: "echo", Arguments: map[string]any{"value": "hello"}})
|
|
if err != nil || result.IsError || calls != 1 {
|
|
t.Fatalf("call %v %v calls %d", result, err, calls)
|
|
}
|
|
if _, err := clientSession.CallTool(t.Context(), &mcp.CallToolParams{Name: "forbidden", Arguments: map[string]any{}}); err == nil {
|
|
t.Fatal("unknown tool accepted")
|
|
}
|
|
if calls != 1 {
|
|
t.Fatal("unknown tool executed")
|
|
}
|
|
samples := 0
|
|
model := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
samples++
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"id":"sample","object":"chat.completion","model":"task","choices":[{"index":0,"message":{"role":"assistant","content":"sampled"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5}}`))
|
|
}))
|
|
defer model.Close()
|
|
llm := modelloop.NewRuntime(map[string]agentrun.Provider{"fixture": {Class: "openai", BaseURL: model.URL + "/v1"}}, nil)
|
|
sample := service.taskSampling(llm, config.Config{Models: map[string]string{"default": "fixture/task"}}, &authority.task, "grant")
|
|
if _, err := sample(t.Context(), protocol.SamplingRequest{MaxTokens: 5, Messages: []protocol.SamplingMessage{{Role: "user", Content: protocol.SamplingContent{Type: "text", Text: "sample"}}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if samples != 1 || len(authority.recorded) != 1 || authority.recorded[0].TaskID != 7 || authority.recorded[0].Source != usage.SourceTask {
|
|
t.Fatal("sampling lost task attribution")
|
|
}
|
|
authority.revoked = true
|
|
result, err = clientSession.CallTool(t.Context(), &mcp.CallToolParams{Name: "echo", Arguments: map[string]any{"value": "again"}})
|
|
if err == nil && !result.IsError {
|
|
t.Fatal("revoked grant executed")
|
|
}
|
|
if calls != 1 {
|
|
t.Fatal("revoked tool executed")
|
|
}
|
|
if _, err := sample(t.Context(), protocol.SamplingRequest{}); err == nil || samples != 1 {
|
|
t.Fatal("revoked grant sampled a model")
|
|
}
|
|
authority.revoked = false
|
|
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
|
|
defer cancel()
|
|
finished := make(chan error, 1)
|
|
go func() {
|
|
_, err := clientSession.CallTool(ctx, &mcp.CallToolParams{Name: "echo", Arguments: map[string]any{"value": "block"}})
|
|
finished <- err
|
|
}()
|
|
select {
|
|
case <-blocked:
|
|
case <-ctx.Done():
|
|
t.Fatal("tool call did not start")
|
|
}
|
|
proxy.Revoke(ctx, session.Token())
|
|
select {
|
|
case <-finished:
|
|
case <-ctx.Done():
|
|
t.Fatal("proxy revocation did not cancel the active call")
|
|
}
|
|
}
|
|
|
|
type taskSentinelTransport struct{ base http.RoundTripper }
|
|
|
|
func (t taskSentinelTransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
r = r.Clone(r.Context())
|
|
r.Header.Set("Authorization", "Bearer "+tasktools.Sentinel)
|
|
return t.base.RoundTrip(r)
|
|
}
|