Files
archie/internal/daemon/model_egress_test.go
sam da1795c597 feat(egress): Docker task containers reach allowed hosts through the egress proxy (#1967)
The task session carries containers.egress_hosts and the profile's remote MCP
hosts, and the container gets ProxyEnv with the relay exempt from the proxy so
the State Store and NATS dials bypass it. A host the session lists exactly may
resolve to a private address; wildcard and any-host matches may not.
egress_hosts moves from openshell to containers, as it applies to both runtimes.
2026-11-14 04:01:31 +11:00

135 lines
4.6 KiB
Go

package daemon
import (
"context"
"encoding/json"
"log/slog"
"testing"
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/eventbus"
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
"github.com/samcharles93/archie-core/internal/taskrun"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// recordingModelEgress keeps the providers a task's model session was opened
// with.
type recordingModelEgress struct {
providers map[string]config.Provider
}
func (r *recordingModelEgress) Open(_ context.Context, _, _ string, providers map[string]config.Provider, _ []string) (ModelSession, error) {
r.providers = providers
return ModelSession{}, nil
}
func (r *recordingModelEgress) Close(context.Context, string) {}
// A task's model session carries only the providers its models name: a
// configured provider no alias reaches must never be required, and the
// provider the task does use arrives with the variable its credential is read
// from.
func TestOpenModelEgressCarriesOnlyReferencedProviders(t *testing.T) {
t.Parallel()
egressProxy := &recordingModelEgress{}
d := wired(&Daemon{
Cfg: config.NewHolder(config.Config{
Models: map[string]string{"default": "used/model-one"},
Providers: map[string]config.Provider{
"used": {Class: "deepseek", APIKey: config.SecretRef{Engine: "env", Key: "USED_KEY"}},
"unused": {Class: "gemini", APIKeyEnv: "UNUSED_KEY"},
},
}),
ModelEgress: egressProxy,
})
task := &workflow.Task{ID: 7, Identity: string(sys), WorkflowDefinitionYAML: "id: w\nsteps:\n - type: agent.run\n"}
if _, _, err := d.openModelEgress(t.Context(), task, "run-credential", nil); err != nil {
t.Fatalf("open model egress: %v", err)
}
if _, ok := egressProxy.providers["unused"]; ok {
t.Fatalf("a provider no model names reached the session: %v", egressProxy.providers)
}
used, ok := egressProxy.providers["used"]
if !ok {
t.Fatalf("the provider the task's model names is missing: %v", egressProxy.providers)
}
if want := config.ProviderKeyEnvName("used"); used.APIKeyEnv != want {
t.Fatalf("provider variable = %q, want %q", used.APIKeyEnv, want)
}
}
// capturingBus records the taskrun request the daemon sent.
type capturingBus struct {
request []byte
reply []byte
}
func (b *capturingBus) PublishUnique(context.Context, string, string, []byte) error { return nil }
func (b *capturingBus) Fetch(context.Context) (eventbus.Message, error) {
return nil, nil
}
func (b *capturingBus) Request(_ context.Context, _ string, payload []byte) ([]byte, error) {
b.request = payload
return b.reply, nil
}
// The provider a task's model runs on reaches the container with a variable the
// agent can read, so the runtime authenticates through the egress proxy instead
// of running the provider unauthenticated.
func TestRunViaAgentCarriesProviderCredentialVariable(t *testing.T) {
reply, err := json.Marshal(taskrun.Response{Status: taskstate.Completed})
if err != nil {
t.Fatalf("marshal response: %v", err)
}
bus := &capturingBus{reply: reply}
d := wired(&Daemon{
Cfg: config.NewHolder(config.Config{
Models: map[string]string{"default": "used/model-one"},
Providers: map[string]config.Provider{
"used": {Class: "deepseek", APIKey: config.SecretRef{Engine: "env", Key: "USED_KEY"}},
},
}),
TaskTransport: bus,
Log: slog.New(slog.DiscardHandler),
})
task := &workflow.Task{ID: 7, Identity: string(sys), Attempt: 1, Workflow: "implement", WorkflowDefinitionYAML: "id: w\nsteps:\n - type: agent.run\n"}
d.runViaAgent(t.Context(), task, config.Repo{}, config.AgentProfile{}, "run-credential")
if bus.request == nil {
t.Fatal("the daemon sent no taskrun request")
}
var req taskrun.Request
if err := json.Unmarshal(bus.request, &req); err != nil {
t.Fatalf("decode taskrun request: %v", err)
}
provider, ok := req.Providers["used"]
if !ok {
t.Fatalf("the task's provider is missing from the request: %v", req.Providers)
}
if provider.APIKeyEnv == "" {
t.Fatalf("the provider reached the container without a credential variable: %+v", provider)
}
sentinel := egress.SentinelFor("used")
t.Setenv(provider.APIKeyEnv, sentinel)
rt := modelloop.NewRuntime(req.Providers, nil)
if rt == nil {
t.Fatal("no runtime built from the request's providers")
}
auth, err := rt.ResolveAuth(t.Context(), "used")
if err != nil {
t.Fatalf("resolve auth: %v", err)
}
if auth.Token != sentinel {
t.Fatalf("the runtime resolves %q, so the egress sentinel is never sent", auth.Token)
}
}