Files
archie/internal/infrastructure/kit/launch_test.go
T
sam 605988e0a9 fix(egress): take a run's credential kind from its binding, not the Kit
The sandbox kit spec lets one credential@1 declare apiKey and oauth
together, meaning "whichever the host has bound": the published
docker/claude-code-kit does exactly that for anthropic, and codex for
openai. archie decided the kind from the Kit's declaration instead
(egress.IsOAuthManaged), so a dual-declared service always came out
OAuth-bound however the org had bound it:

  - kitrun.resolveCredentials granted an API-key binding's service the
    empty string, so the proxy had no key to inject and every request to
    a domain the Kit named carried an empty credential. The reference
    Claude Code Kit could not authenticate with an API key at all.
  - an API-key-only service whose binding named no secret resolved to ""
    through the zero SecretRef, presenting an empty credential rather than
    staying unbound.
  - the proxy fired an apiKey inject rule for an OAuth-bound service,
    putting "Bearer " on the wire, and intercepted the token endpoint of
    an API-key-bound one, whose only outcomes were a 502 on the missing
    token set, a capture the org never bound, or -- for a required
    dual-declared credential -- a refused launch when no token store was
    configured.
  - the container's SBX_CRED_*_MODE said "apikey" for an OAuth-bound
    dual-declared service, and kitrun demanded a captured OAuth token for
    an API-key-bound one whose Kit renders a credential file.

One root cause, fixed once: the org's binding decides the kind. It is
now a first-class fact (egress.CredentialKind: apikey or oauth, absent
meaning unbound), computed from the binding in resolveCredentials and
carried to every consumer of it -- the resolver grant, the container's
credential state (mode variable and credential file), and the proxy
session, whose rule sets are built from it so a rule can only fire for a
service the run carries as that same kind. A service the run does not
carry at all keeps both rule sets, so resolving it still refuses a
required credential and skips an optional one.

Declared and bound stay two independent facts, as the reported
declared-and-granted intersection requires: neither widens the other.
No secret value crosses into kit (Bound carries kinds only) and none is
logged or written to a task row.
2026-10-02 09:37:20 +10:00

223 lines
7.5 KiB
Go

package kit
import (
"slices"
"strings"
"testing"
"github.com/docker/sandbox-kit-spec/v3/spec"
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
)
const launchKit = `
- type: com.docker.sandbox/agent-sessions@1
config:
prompt: [-p, "{{.Prompt}}"]
resume: [--resume, "{{.SessionID}}"]
- type: com.docker.sandbox/network-policy@1
config: {install: {allow: [registry.example.com]}, runtime: {allow: [api.example.com]}}
- type: com.docker.sandbox/credential@1
config:
service: example
phase: runtime
apiKey: {name: EXAMPLE_KEY, proxyManaged: true, inject: [{domain: api.example.com, header: x-api-key, format: "%s"}]}
- type: com.docker.sandbox/volume@1
config: {path: /home/agent/.cli/sessions, size: 1g}
- type: com.docker.sandbox/volume@1
config: {path: /home/agent/.cli/history}
- type: com.docker.sandbox/lifecycle@1
config:
install:
- command: echo "$WORKSPACE_DIR" > /etc/seeded
env: [WORKSPACE_DIR]
startup:
- command: [cli, warm-up]
env: [SBX_CRED_EXAMPLE_MODE, NOT_A_RUNTIME_VARIABLE]
- command: [cli, daemon]
user: "0"
background: true`
func plan(t *testing.T) *Plan {
t.Helper()
p, err := admit(t, spec.Contribution{Reference: "kit", Descriptor: descriptor(t, spec.KindWorkload, launchKit)})
if err != nil {
t.Fatalf("admit: %v", err)
}
return p
}
var image = ImageConfig{
Entrypoint: []string{"/usr/local/bin/cli"},
Cmd: []string{"--no-color"},
Env: []string{"PATH=/usr/local/bin:/usr/bin:/bin", "HOME=/home/agent", "IMAGE_ONLY=kept"},
User: "agent",
}
var params = LaunchParams{Execution: "exec-42", ProxyToken: "tok", CAPath: "/etc/archie/ca.pem", Bound: map[string]egress.CredentialKind{"example": egress.CredentialAPIKey}}
func TestAssembleHarness(t *testing.T) {
l, err := Assemble(plan(t), image, params)
if err != nil {
t.Fatal(err)
}
h := l.Harness
if !slices.Equal(h.Launch, []string{"/usr/local/bin/cli", "--no-color"}) {
t.Errorf("launch %v, want the image entrypoint then cmd", h.Launch)
}
if !slices.Equal(h.Prompt, []string{"-p", "{{.Prompt}}"}) || !slices.Equal(h.Resume, []string{"--resume", "{{.SessionID}}"}) {
t.Errorf("verbs prompt=%v resume=%v, want the Kit's agent-sessions verbs", h.Prompt, h.Resume)
}
if h.User != "agent" {
t.Errorf("user %q, want the image's user", h.User)
}
for _, want := range []string{
"PATH=/usr/local/bin:/usr/bin:/bin", "IMAGE_ONLY=kept",
"WORKSPACE_DIR=" + WorkspaceDir,
"HTTPS_PROXY=http://archie:tok@archie-egress:3128",
"SSL_CERT_FILE=/etc/archie/ca.pem",
"EXAMPLE_KEY=archie-proxy-managed",
} {
if !slices.Contains(h.Env, want) {
t.Errorf("harness env lacks %q:\n%s", want, strings.Join(h.Env, "\n"))
}
}
}
func TestAssembleNeverRunsTheHarnessAsRoot(t *testing.T) {
for _, user := range []string{"", "root", "0", "0:0"} {
img := image
img.User = user
l, err := Assemble(plan(t), img, params)
if err != nil {
t.Fatal(err)
}
if l.Harness.User != DefaultHarnessUser {
t.Errorf("image user %q gave harness user %q, want %q", user, l.Harness.User, DefaultHarnessUser)
}
}
}
func TestAssembleHooks(t *testing.T) {
l, err := Assemble(plan(t), image, params)
if err != nil {
t.Fatal(err)
}
if len(l.Install) != 1 || len(l.Startup) != 2 {
t.Fatalf("install %d startup %d, want 1 and 2", len(l.Install), len(l.Startup))
}
install := l.Install[0]
if !slices.Equal(install.Argv, []string{"sh", "-c", `echo "$WORKSPACE_DIR" > /etc/seeded`}) {
t.Errorf("a string command runs through the shell, as the Kit spec decodes it; got %v", install.Argv)
}
if install.User != "0" {
t.Errorf("install hook user %q, want root by default", install.User)
}
if !slices.Contains(install.Env, "WORKSPACE_DIR="+WorkspaceDir) || !slices.Contains(install.Env, "HTTPS_PROXY=http://archie:tok@archie-egress:3128") {
t.Errorf("install env %v, want its declared variable and the egress baseline", install.Env)
}
warm := l.Startup[0]
if warm.User != DefaultHarnessUser {
t.Errorf("startup hook user %q, want the agent user by default", warm.User)
}
if !slices.Contains(warm.Env, "SBX_CRED_EXAMPLE_MODE=apikey") {
t.Errorf("startup env %v, want the bound credential's mode", warm.Env)
}
for _, e := range warm.Env {
if strings.HasPrefix(e, "NOT_A_RUNTIME_VARIABLE=") || strings.HasPrefix(e, "IMAGE_ONLY=") || strings.HasPrefix(e, "EXAMPLE_KEY=") {
t.Errorf("startup env carries %q, which it neither declared nor is baseline", e)
}
}
if daemon := l.Startup[1]; daemon.User != "0" || !daemon.Background {
t.Errorf("startup hook %+v, want its declared root user and background flag", daemon)
}
}
func TestAssembleVolumesArePerExecution(t *testing.T) {
a, err := Assemble(plan(t), image, params)
if err != nil {
t.Fatal(err)
}
again, _ := Assemble(plan(t), image, params)
other := params
other.Execution = "exec-43"
b, _ := Assemble(plan(t), image, other)
if len(a.Volumes) != 2 {
t.Fatalf("volumes %v, want 2", a.Volumes)
}
if !slices.Equal(a.Volumes, again.Volumes) {
t.Error("volume names are not stable for one execution; a retry would lose its sessions")
}
if a.Volumes[0].Name == a.Volumes[1].Name {
t.Error("two paths share a volume")
}
for i := range a.Volumes {
if a.Volumes[i].Name == b.Volumes[i].Name {
t.Errorf("executions share volume %q", a.Volumes[i].Name)
}
}
}
func TestAssembleAgentContextProfile(t *testing.T) {
assemble := func(t *testing.T, contextYAML string) Launch {
t.Helper()
p, err := admit(t, spec.Contribution{Reference: "registry/team-set@sha256:ab", Descriptor: descriptor(t, spec.KindWorkload, sessions+contextYAML)})
if err != nil {
t.Fatalf("admit: %v", err)
}
l, err := Assemble(p, image, LaunchParams{Execution: "exec-1"})
if err != nil {
t.Fatalf("Assemble: %v", err)
}
return l
}
byPath := func(files []spec.File) map[string]string {
out := map[string]string{}
for _, f := range files {
out[f.Path] = f.Content
}
return out
}
t.Run("a staged body is indexed where the image holds it", func(t *testing.T) {
files := byPath(assemble(t, `
- type: com.docker.sandbox/agent-context@1
config: {filename: CLAUDE.md, contentFile: /usr/share/sandbox/kit/team/context.md}`).Context)
profile, ok := files["/archie/CLAUDE.md"]
if !ok || len(files) != 1 {
t.Fatalf("context files = %v, want only the profile beside the workspace", files)
}
if !strings.Contains(profile, "registry/team-set@sha256:ab") || !strings.Contains(profile, "/usr/share/sandbox/kit/team/context.md") {
t.Fatalf("profile does not index the Kit's staged body:\n%s", profile)
}
})
t.Run("an inline body gets its own file the profile points at", func(t *testing.T) {
files := byPath(assemble(t, `
- type: com.docker.sandbox/agent-context@1
config: {filename: AGENTS.md, content: "Use motd."}`).Context)
profile := files["/archie/AGENTS.md"]
var body string
for path, content := range files {
if path != "/archie/AGENTS.md" {
body = path
if content != "Use motd." {
t.Fatalf("%s = %q, want the inline body", path, content)
}
}
}
if body == "" || strings.HasPrefix(body, WorkspaceDir+"/") || !strings.Contains(profile, body) {
t.Fatalf("files %v: want the inline body outside the workspace and indexed by the profile", files)
}
if strings.Contains(profile, "Use motd.") {
t.Fatal("the profile inlines a Kit's body; it must only point at it")
}
})
t.Run("no profile filename materializes nothing", func(t *testing.T) {
if files := assemble(t, "").Context; len(files) != 0 {
t.Fatalf("context files = %v, want none", files)
}
})
}