Files
archie/internal/infrastructure/kit/credential_scope_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

134 lines
4.8 KiB
Go

package kit
import (
"encoding/json"
"strings"
"testing"
"github.com/docker/sandbox-kit-spec/v3/spec"
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
)
// claudeCodeCredentialFileKit mirrors the published Claude Code Kit's
// credentialFile: the OAuth fields nested, the granted scopes among them, and
// a top-level primaryApiKey.
const claudeCodeCredentialFileKit = sessions + `
- type: com.docker.sandbox/credential@1
config:
service: claude-code
phase: runtime
oauth:
tokenEndpoint: {host: platform.claude.com, path: /v1/oauth/token}
sentinels:
accessToken: sk-ant-oat01-proxy-managed
refreshToken: sk-ant-ort01-proxy-managed
credentialFile:
path: /home/agent/.claude/.credentials.json
structure:
claudeAiOauth:
accessToken: "{{.AccessToken}}"
refreshToken: "{{.RefreshToken}}"
expiresAt: "{{.ExpiresAt}}"
scopes: "{{.Scopes}}"
primaryApiKey: "{{.PrimaryApiKey}}"`
// renderClaudeCredentialFile assembles the Claude-shaped Kit with facts and
// returns the credential file's decoded top level.
func renderClaudeCredentialFile(t *testing.T, facts OAuthFacts) map[string]any {
t.Helper()
p, err := Admit(descriptor(t, spec.KindWorkload, claudeCodeCredentialFileKit))
if err != nil {
t.Fatalf("admit: %v", err)
}
l, err := Assemble(p, image, LaunchParams{
Execution: "exec-42", Bound: map[string]egress.CredentialKind{"claude-code": egress.CredentialOAuth},
OAuth: map[string]OAuthFacts{"claude-code": facts},
})
if err != nil {
t.Fatalf("Assemble: %v", err)
}
f, ok := findFile(l.Files, "/home/agent/.claude/.credentials.json")
if !ok {
t.Fatalf("no credential file at the declared path; files = %v", l.Files)
}
var doc map[string]any
if err := json.Unmarshal([]byte(f.Content), &doc); err != nil {
t.Fatalf("credential file is not JSON: %v\n%s", err, f.Content)
}
return doc
}
// TestAssembleRendersTheGrantedScopes: {{.Scopes}} is an array in the target
// encoding, so a one-element set must still be a one-element ARRAY and never
// collapse to a bare string -- the single and multi cases differ exactly
// there, and nowhere else.
func TestAssembleRendersTheGrantedScopes(t *testing.T) {
tests := []struct {
name string
scopes []string
}{
{name: "a single granted scope is a one-element array", scopes: []string{"user:inference"}},
{name: "several granted scopes are a multi-element array", scopes: []string{"user:inference", "user:profile"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc := renderClaudeCredentialFile(t, OAuthFacts{Scopes: tt.scopes})
oauth, _ := doc["claudeAiOauth"].(map[string]any)
if oauth == nil {
t.Fatalf("claudeAiOauth = %v, want an object", doc["claudeAiOauth"])
}
raw, ok := oauth["scopes"].([]any)
if !ok {
t.Fatalf("scopes = %#v (%T), want a JSON array", oauth["scopes"], oauth["scopes"])
}
if len(raw) != len(tt.scopes) {
t.Fatalf("scopes = %v, want %v", raw, tt.scopes)
}
for i, want := range tt.scopes {
if raw[i] != want {
t.Fatalf("scopes[%d] = %v, want %q", i, raw[i], want)
}
}
})
}
}
// TestAssembleRefusesWhenNoScopesWereCaptured: the spec declares no omission
// case for {{.Scopes}}, so a token set with no scopes refuses the launch with
// something the operator can act on rather than writing an empty array, which
// would silently change the CLI's capability decisions.
func TestAssembleRefusesWhenNoScopesWereCaptured(t *testing.T) {
p, err := Admit(descriptor(t, spec.KindWorkload, claudeCodeCredentialFileKit))
if err != nil {
t.Fatalf("admit: %v", err)
}
_, err = Assemble(p, image, LaunchParams{
Execution: "exec-42", Bound: map[string]egress.CredentialKind{"claude-code": egress.CredentialOAuth},
OAuth: map[string]OAuthFacts{"claude-code": {}},
})
if err == nil {
t.Fatal("Assemble with no captured scopes = nil error, want a refusal")
}
for _, want := range []string{"{{.Scopes}}", "setup terminal"} {
if !strings.Contains(err.Error(), want) {
t.Fatalf("Assemble error = %q, want it to name %q", err, want)
}
}
}
// TestAssembleOmitsAnUncapturedPrimaryApiKey: the spec's case for
// {{.PrimaryApiKey}} is that its enclosing key is omitted when no key is
// captured. archie never hands the container a key -- it resolves one only to
// fill the egress grant -- so the key must be ABSENT; absent and empty are
// different bugs.
func TestAssembleOmitsAnUncapturedPrimaryApiKey(t *testing.T) {
doc := renderClaudeCredentialFile(t, OAuthFacts{Scopes: []string{"user:inference"}})
if v, present := doc["primaryApiKey"]; present {
t.Fatalf("primaryApiKey = %#v, want the key omitted entirely", v)
}
if _, ok := doc["claudeAiOauth"]; !ok {
t.Fatalf("claudeAiOauth missing alongside primaryApiKey: %v", doc)
}
}