Runtimes now take a task launch: image, mounts, env, the daemon's NATS and State Store with the task's credentials, its model providers and the hosts its tools may reach. Docker addresses them through the egress relay as before. An OpenShell sandbox reaches NATS and the State Store over plain TCP, the model providers with their credential bound, and the task's remote MCP hosts and the allowed package registries; services addressed by IP or loopback are named by the configured services host, since sandbox policy admits only DNS names.
111 lines
3.9 KiB
Go
111 lines
3.9 KiB
Go
package openshell
|
|
|
|
import (
|
|
"maps"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
v1 "github.com/ai-openshell/sdk-go/openshell/v1"
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
|
)
|
|
|
|
// TestSandboxPolicyAdmitsOnlyWhatTheTaskNeeds guards the sandbox's egress
|
|
// boundary: archie's own services and the model providers are open to the
|
|
// agent alone, extra hosts to any tool, nothing else is admitted, services
|
|
// addressed by IP or loopback are named by the services host, and no
|
|
// provider key enters the environment.
|
|
func TestSandboxPolicyAdmitsOnlyWhatTheTaskNeeds(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
servicesHost string
|
|
launch Launch
|
|
wantErr string
|
|
wantRules map[string]string // rule name -> "host:port binary"
|
|
wantEnv map[string]string
|
|
}{
|
|
{
|
|
name: "services by IP and loopback use the services host",
|
|
servicesHost: "archie.lan",
|
|
launch: Launch{Worker: egress.Endpoints{
|
|
NATS: "nats://172.17.0.1:4222", NATSToken: "t", StateStore: "dns:///127.0.0.1:50051", StateStoreToken: "run-1",
|
|
}},
|
|
wantRules: map[string]string{
|
|
"archie-nats": "archie.lan:4222 " + agentBinary,
|
|
"archie-state-store": "archie.lan:50051 " + agentBinary,
|
|
},
|
|
wantEnv: map[string]string{
|
|
"NATS_URL": "nats://archie.lan:4222", "NATS_TOKEN": "t",
|
|
"STATE_STORE_URL": "archie.lan:50051", "STATE_STORE_TOKEN": "run-1",
|
|
},
|
|
},
|
|
{
|
|
name: "services by DNS name, providers and extra hosts",
|
|
launch: Launch{
|
|
Env: []string{"WORKTREE_UID=1000"},
|
|
Worker: egress.Endpoints{NATS: "nats://nats.internal:4222"},
|
|
Providers: []Provider{{Gateway: "task-7-anthropic", Host: "api.anthropic.com", Port: 443}},
|
|
Hosts: []string{"proxy.golang.org", "mcp.lan:8443"},
|
|
},
|
|
wantRules: map[string]string{
|
|
"archie-nats": "nats.internal:4222 " + agentBinary,
|
|
"model-task-7-anthropic": "api.anthropic.com:443 " + agentBinary,
|
|
"egress": "proxy.golang.org:443,mcp.lan:8443 " + anyBinary,
|
|
},
|
|
wantEnv: map[string]string{"WORKTREE_UID": "1000", "NATS_URL": "nats://nats.internal:4222"},
|
|
},
|
|
{
|
|
name: "a service by IP with no services host is refused",
|
|
launch: Launch{Worker: egress.Endpoints{NATS: "nats://10.0.0.5:4222"}},
|
|
wantErr: "services host",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
rt := New(nil, Options{Instance: "archie", ServicesHost: tt.servicesHost})
|
|
spec, err := rt.spec(tt.launch)
|
|
if tt.wantErr != "" {
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Fatalf("spec error = %v, want one mentioning %q", err, tt.wantErr)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]string{}
|
|
for name, rule := range spec.Policy.NetworkPolicies {
|
|
var hosts, binaries []string
|
|
for _, e := range rule.Endpoints {
|
|
hosts = append(hosts, e.Host+":"+strconv.Itoa(int(e.Port)))
|
|
if strings.HasPrefix(name, "model-") && (e.CredentialBinding == nil || e.CredentialBinding.Provider != strings.TrimPrefix(name, "model-")) {
|
|
t.Errorf("rule %s does not bind its provider's credential", name)
|
|
}
|
|
if strings.HasPrefix(name, "archie-") && (e.Protocol != "tcp" || e.TLS != v1.NetworkTLSModeSkip) {
|
|
t.Errorf("rule %s is not plain TCP left uninspected", name)
|
|
}
|
|
}
|
|
for _, b := range rule.Binaries {
|
|
binaries = append(binaries, b.Path)
|
|
}
|
|
got[name] = strings.Join(hosts, ",") + " " + strings.Join(binaries, ",")
|
|
}
|
|
if !maps.Equal(got, tt.wantRules) {
|
|
t.Fatalf("rules = %v, want %v", got, tt.wantRules)
|
|
}
|
|
if !maps.Equal(spec.Environment, tt.wantEnv) {
|
|
t.Fatalf("environment = %v, want %v", spec.Environment, tt.wantEnv)
|
|
}
|
|
var providers []string
|
|
for _, p := range tt.launch.Providers {
|
|
providers = append(providers, p.Gateway)
|
|
}
|
|
if !slices.Equal(spec.Providers, providers) {
|
|
t.Fatalf("attached providers = %v, want %v", spec.Providers, providers)
|
|
}
|
|
})
|
|
}
|
|
}
|