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.
223 lines
7.9 KiB
Go
223 lines
7.9 KiB
Go
package egress
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"errors"
|
|
"net/http"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/docker/sandbox-kit-spec/v3/spec"
|
|
)
|
|
|
|
func apiKey(service, phase string, required bool, name string, inject ...spec.Inject) spec.CredentialCapability {
|
|
return spec.CredentialCapability{
|
|
Service: service, Phase: phase, APIKey: &spec.APIKey{Name: name, ProxyManaged: true, Inject: inject},
|
|
Required: required,
|
|
}
|
|
}
|
|
|
|
// registerWith registers run-7 with every declared service carried as an API
|
|
// key, mirroring what the launcher passes for a run whose bindings all name
|
|
// secrets. registerBoundWith is for the tests that need a different kind.
|
|
func (h *harness) registerWith(t *testing.T, creds ...spec.CredentialCapability) *Session {
|
|
t.Helper()
|
|
kinds := map[string]CredentialKind{}
|
|
for _, c := range creds {
|
|
kinds[c.Service] = CredentialAPIKey
|
|
}
|
|
return h.registerBoundWith(t, kinds, creds...)
|
|
}
|
|
|
|
func (h *harness) registerBoundWith(t *testing.T, bound map[string]CredentialKind, creds ...spec.CredentialCapability) *Session {
|
|
t.Helper()
|
|
s, err := h.proxy.Register(SessionOptions{
|
|
Run: "run-7",
|
|
Network: &spec.PhasedNetwork{
|
|
Install: &spec.NetworkRules{Allow: []string{"install.example.com"}},
|
|
Runtime: &spec.NetworkRules{Allow: []string{"api.example.com:443", "plain.example.com:80"}},
|
|
},
|
|
Credentials: creds,
|
|
Bound: bound,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func TestInjectsAnAPIKeyHeader(t *testing.T) {
|
|
h := newHarness(t)
|
|
h.bind("run-7/example", "real-secret")
|
|
s := h.registerWith(t, apiKey("example", "runtime", true, "EXAMPLE_KEY",
|
|
spec.Inject{Domain: "api.example.com", Header: "Authorization", Format: "Bearer %s"}))
|
|
s.EnterRuntime()
|
|
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, "https://api.example.com/", nil)
|
|
req.Header.Set("Authorization", "Bearer "+Sentinel)
|
|
resp, err := h.client(s.Token()).Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = resp.Body.Close()
|
|
if got := h.lastAuth.Load(); got != "Bearer real-secret" {
|
|
t.Fatalf("upstream Authorization %v, want the real key in place of the sentinel", got)
|
|
}
|
|
}
|
|
|
|
func TestInjectsBasicAuth(t *testing.T) {
|
|
h := newHarness(t)
|
|
h.bind("run-7/example", "real-secret")
|
|
s := h.registerWith(t, apiKey("example", "runtime", true, "",
|
|
spec.Inject{Domain: "api.example.com", Scheme: "basic", Username: "x-access-token"}))
|
|
s.EnterRuntime()
|
|
if _, _, err := get(t, h.client(s.Token()), "https://api.example.com/"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := "Basic " + base64.StdEncoding.EncodeToString([]byte("x-access-token:real-secret"))
|
|
if got := h.lastAuth.Load(); got != want {
|
|
t.Fatalf("upstream Authorization %v, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestInjectionIsScopedToItsDomainAndPhase(t *testing.T) {
|
|
h := newHarness(t)
|
|
h.bind("run-7/example", "real-secret")
|
|
h.bind("run-7/installer", "install-secret")
|
|
s := h.registerWith(t,
|
|
apiKey("example", "runtime", true, "EXAMPLE_KEY", spec.Inject{Domain: "api.example.com", Header: "Authorization", Format: "Bearer %s"}),
|
|
apiKey("installer", "install", true, "", spec.Inject{Domain: "plain.example.com", Header: "X-Install", Format: "%s"}),
|
|
)
|
|
s.EnterRuntime()
|
|
if _, _, err := get(t, h.client(s.Token()), "http://plain.example.com/"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := h.lastAuth.Load(); got != "" {
|
|
t.Fatalf("a key for api.example.com was sent to plain.example.com: %v", got)
|
|
}
|
|
if h.lastInstall.Load() != "" {
|
|
t.Fatal("an install-phase credential was injected at runtime")
|
|
}
|
|
}
|
|
|
|
func TestAnUnboundCredential(t *testing.T) {
|
|
t.Run("required stops the request before it reaches upstream", func(t *testing.T) {
|
|
h := newHarness(t)
|
|
s := h.registerWith(t, apiKey("example", "runtime", true, "EXAMPLE_KEY",
|
|
spec.Inject{Domain: "api.example.com", Header: "Authorization", Format: "Bearer %s"}))
|
|
s.EnterRuntime()
|
|
status, _, err := get(t, h.client(s.Token()), "https://api.example.com/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if status != http.StatusBadGateway || h.hits.Load() != 0 {
|
|
t.Fatalf("status %d with %d upstream hits; want 502 and none", status, h.hits.Load())
|
|
}
|
|
})
|
|
t.Run("optional passes without one", func(t *testing.T) {
|
|
h := newHarness(t)
|
|
s := h.registerWith(t, apiKey("example", "runtime", false, "EXAMPLE_KEY",
|
|
spec.Inject{Domain: "api.example.com", Header: "Authorization", Format: "Bearer %s"}))
|
|
s.EnterRuntime()
|
|
status, _, err := get(t, h.client(s.Token()), "https://api.example.com/")
|
|
if err != nil || status != http.StatusOK {
|
|
t.Fatalf("got %d %v", status, err)
|
|
}
|
|
if got := h.lastAuth.Load(); got != "" {
|
|
t.Fatalf("upstream saw Authorization %v with no binding", got)
|
|
}
|
|
})
|
|
t.Run("a resolver failure is never treated as unbound", func(t *testing.T) {
|
|
h := newHarness(t)
|
|
h.failResolves(errors.New("state store unreachable"))
|
|
s := h.registerWith(t, apiKey("example", "runtime", false, "EXAMPLE_KEY",
|
|
spec.Inject{Domain: "api.example.com", Header: "Authorization", Format: "Bearer %s"}))
|
|
s.EnterRuntime()
|
|
status, _, err := get(t, h.client(s.Token()), "https://api.example.com/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if status != http.StatusBadGateway || h.hits.Load() != 0 {
|
|
t.Fatalf("status %d with %d upstream hits; want 502 and none", status, h.hits.Load())
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTheResolverSeesTheRunAndService(t *testing.T) {
|
|
h := newHarness(t)
|
|
h.bind("run-7/example", "real-secret")
|
|
s := h.registerWith(t, apiKey("example", "runtime", true, "EXAMPLE_KEY",
|
|
spec.Inject{Domain: "api.example.com", Header: "Authorization", Format: "Bearer %s"}))
|
|
s.EnterRuntime()
|
|
if _, _, err := get(t, h.client(s.Token()), "https://api.example.com/"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
if !slices.Contains(h.resolved, "run-7/example") {
|
|
t.Fatalf("resolver calls %v, want run-7/example", h.resolved)
|
|
}
|
|
}
|
|
|
|
// TestAnOAuthBoundServiceInjectsNoAPIKey is the "whichever the host has
|
|
// bound" side of the credential kind: docker/claude-code-kit declares apiKey
|
|
// and oauth on one service, and an org that bound the OAuth token set must
|
|
// not have the API-key rule fire with the empty value the resolver answers
|
|
// for an OAuth-bound service -- every request to a domain the Kit named
|
|
// would then carry an empty credential.
|
|
func TestAnOAuthBoundServiceInjectsNoAPIKey(t *testing.T) {
|
|
h := newHarness(t)
|
|
// The run carries the service as the org's OAuth token set: the resolver
|
|
// answers it (with no API-key value, as kitrun grants an OAuth-bound
|
|
// service).
|
|
h.bind("run-7/example", "")
|
|
s := h.registerBoundWith(t, map[string]CredentialKind{"example": CredentialOAuth},
|
|
apiKey("example", "runtime", true, "EXAMPLE_KEY",
|
|
spec.Inject{Domain: "api.example.com", Header: "Authorization", Format: "Bearer %s"}))
|
|
s.EnterRuntime()
|
|
if _, _, err := get(t, h.client(s.Token()), "https://api.example.com/"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := h.lastAuth.Load(); got != "" {
|
|
t.Fatalf("upstream Authorization %v, want no header: an OAuth-bound service carries no API key", got)
|
|
}
|
|
}
|
|
|
|
func TestSentinelEnv(t *testing.T) {
|
|
env := SentinelEnv([]spec.CredentialCapability{
|
|
apiKey("named", "runtime", true, "NAMED_KEY", spec.Inject{Domain: "a.example.com", Header: "X", Format: "%s"}),
|
|
apiKey("injectonly", "runtime", true, "", spec.Inject{Domain: "b.example.com", Header: "X", Format: "%s"}),
|
|
})
|
|
if !slices.Equal(env, []string{"NAMED_KEY=" + Sentinel}) {
|
|
t.Fatalf("SentinelEnv = %v; a named key gets the sentinel, an inject-only key gets nothing", env)
|
|
}
|
|
}
|
|
|
|
// fakeResolver lets the harness stand in for the run credential's secrets.
|
|
func (h *harness) resolve(_ context.Context, run, service string) (string, error) {
|
|
key := run + "/" + service
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
h.resolved = append(h.resolved, key)
|
|
if h.resolveErr != nil {
|
|
return "", h.resolveErr
|
|
}
|
|
v, ok := h.secrets[key]
|
|
if !ok {
|
|
return "", ErrUnbound
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
func (h *harness) bind(key, value string) {
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
h.secrets[key] = value
|
|
}
|
|
|
|
func (h *harness) failResolves(err error) {
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
h.resolveErr = err
|
|
}
|