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.
This commit is contained in:
1 parent
2c5dfe51a7
commit
2e1ab386fb
12 files changed
+314
-66
No files matched your search
@@ -14,6 +14,24 @@ import (
|
||||
// it can tell a credential is wired without ever holding it.
|
||||
const Sentinel = "archie-proxy-managed"
|
||||
|
||||
// CredentialKind is how one run carries a credential service. The org's
|
||||
// binding decides it, never the Kit's declaration: the sandbox kit spec lets
|
||||
// one credential@1 declare apiKey and oauth together, meaning "whichever the
|
||||
// host has bound" (docker/claude-code-kit declares both for anthropic, codex
|
||||
// for openai). A service absent from a session's bound kinds is not carried
|
||||
// at all, which is what refuses a required credential and skips an optional
|
||||
// one.
|
||||
type CredentialKind string
|
||||
|
||||
const (
|
||||
// CredentialAPIKey is an API key the proxy injects from the org secret
|
||||
// the binding names.
|
||||
CredentialAPIKey CredentialKind = "apikey"
|
||||
// CredentialOAuth is the org's captured OAuth token set, which the proxy
|
||||
// holds: the binding names no secret of its own.
|
||||
CredentialOAuth CredentialKind = "oauth"
|
||||
)
|
||||
|
||||
// ErrUnbound reports that the run has no binding for a service. It is the
|
||||
// only resolver error an optional credential may pass over; any other error
|
||||
// stops the request.
|
||||
@@ -45,10 +63,14 @@ type injection struct {
|
||||
username string
|
||||
}
|
||||
|
||||
func compileInjections(creds []spec.CredentialCapability) []injection {
|
||||
// compileInjections compiles the credential@1 apiKey inject rules this run
|
||||
// can fire. A service the run carries as OAuth contributes none: its value is
|
||||
// the org's token set, which the proxy holds and injects on its own paths, so
|
||||
// an API-key rule for it would only put an empty credential on the wire.
|
||||
func compileInjections(creds []spec.CredentialCapability, bound map[string]CredentialKind) []injection {
|
||||
var out []injection
|
||||
for _, c := range creds {
|
||||
if c.APIKey == nil {
|
||||
if c.APIKey == nil || bound[c.Service] == CredentialOAuth {
|
||||
continue
|
||||
}
|
||||
for _, rule := range c.APIKey.Inject {
|
||||
|
||||
@@ -18,7 +18,19 @@ func apiKey(service, phase string, required bool, name string, inject ...spec.In
|
||||
}
|
||||
}
|
||||
|
||||
// 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",
|
||||
@@ -27,6 +39,7 @@ func (h *harness) registerWith(t *testing.T, creds ...spec.CredentialCapability)
|
||||
Runtime: &spec.NetworkRules{Allow: []string{"api.example.com:443", "plain.example.com:80"}},
|
||||
},
|
||||
Credentials: creds,
|
||||
Bound: bound,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -146,6 +159,30 @@ func TestTheResolverSeesTheRunAndService(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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"}),
|
||||
|
||||
@@ -50,10 +50,14 @@ func IsOAuthManaged(c spec.CredentialCapability) bool {
|
||||
return c.OAuth != nil && !c.OAuth.Passthrough && c.OAuth.TokenEndpoint != nil
|
||||
}
|
||||
|
||||
func compileOAuthRules(creds []spec.CredentialCapability) []oauthRule {
|
||||
// compileOAuthRules compiles the credential@1 OAuth rules this run can
|
||||
// exercise. A service the run carries as an API key contributes none: its
|
||||
// token endpoint is not a path this run has a token set to exchange, so
|
||||
// intercepting it could only try to capture one the org never bound.
|
||||
func compileOAuthRules(creds []spec.CredentialCapability, bound map[string]CredentialKind) []oauthRule {
|
||||
var out []oauthRule
|
||||
for _, c := range creds {
|
||||
if !IsOAuthManaged(c) {
|
||||
if !IsOAuthManaged(c) || bound[c.Service] == CredentialAPIKey {
|
||||
continue
|
||||
}
|
||||
rule := oauthRule{
|
||||
|
||||
@@ -95,6 +95,7 @@ func (h *harness) oauthSession(t *testing.T, required, grant bool) *Session {
|
||||
Run: "run-7", Org: "org-1",
|
||||
Network: &spec.PhasedNetwork{Runtime: &spec.NetworkRules{Allow: []string{"oauth.example.com:443", "api.example.com:443"}}},
|
||||
Credentials: []spec.CredentialCapability{oauthCred(required)},
|
||||
Bound: map[string]CredentialKind{"claude-code": CredentialOAuth},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -255,13 +256,68 @@ func TestOAuthResourceRequestCarriesTheStoredAccessToken(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// dualCred declares apiKey and oauth on one service, the spec's shape for
|
||||
// "whichever the host has bound" (docker/claude-code-kit does, for
|
||||
// anthropic).
|
||||
func dualCred(required bool) spec.CredentialCapability {
|
||||
c := oauthCred(required)
|
||||
c.APIKey = &spec.APIKey{Name: "EXAMPLE_KEY", ProxyManaged: true, Inject: []spec.Inject{
|
||||
{Domain: "api.example.com", Header: "Authorization", Format: "Bearer %s"},
|
||||
}}
|
||||
return c
|
||||
}
|
||||
|
||||
// TestAnAPIKeyBoundServiceDoesNotInterceptTheTokenEndpoint is the mirror of
|
||||
// TestAnOAuthBoundServiceInjectsNoAPIKey: when the org's binding names a
|
||||
// secret for a service whose Kit also declares OAuth, this run has no token
|
||||
// set to exchange, so its token endpoint is an ordinary allowed host.
|
||||
// Intercepting it could only fail on the missing token set, or capture a
|
||||
// login the org never bound.
|
||||
func TestAnAPIKeyBoundServiceDoesNotInterceptTheTokenEndpoint(t *testing.T) {
|
||||
h := newHarness(t)
|
||||
h.bind("run-7/claude-code", "sk-real-key")
|
||||
s, err := h.proxy.Register(SessionOptions{
|
||||
Run: "run-7", Org: "org-1",
|
||||
Network: &spec.PhasedNetwork{Runtime: &spec.NetworkRules{Allow: []string{"oauth.example.com:443"}}},
|
||||
Credentials: []spec.CredentialCapability{dualCred(false)},
|
||||
Bound: map[string]CredentialKind{"claude-code": CredentialAPIKey},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.EnterRuntime()
|
||||
status, _ := postToken(t, h, s, "grant_type=refresh_token&refresh_token="+sentinelRefresh)
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("token endpoint status %d, want 200: an API-key-bound run has no token set to intercept", status)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegisterAllowsARequiredAPIKeyBoundCredentialWithNoStore: a Kit may
|
||||
// declare apiKey and oauth on one service as required (the spec's
|
||||
// "whichever the host has bound"). An org that bound the API key has no use
|
||||
// for a token store, so the launch must not be refused for the missing one.
|
||||
func TestRegisterAllowsARequiredAPIKeyBoundCredentialWithNoStore(t *testing.T) {
|
||||
ca, err := LoadOrCreateCA(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := NewProxy(ca, ProxyOptions{})
|
||||
if _, err := p.Register(SessionOptions{
|
||||
Run: "run-7", Org: "org-1",
|
||||
Credentials: []spec.CredentialCapability{dualCred(true)},
|
||||
Bound: map[string]CredentialKind{"claude-code": CredentialAPIKey},
|
||||
}); err != nil {
|
||||
t.Fatalf("Register() = %v, want an API-key-bound run to need no token store", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterRefusesARequiredOAuthCredentialWithNoStore(t *testing.T) {
|
||||
ca, err := LoadOrCreateCA(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := NewProxy(ca, ProxyOptions{})
|
||||
if _, err := p.Register(SessionOptions{Run: "run-7", Org: "org-1", Credentials: []spec.CredentialCapability{oauthCred(true)}}); err == nil {
|
||||
if _, err := p.Register(SessionOptions{Run: "run-7", Org: "org-1", Credentials: []spec.CredentialCapability{oauthCred(true)}, Bound: map[string]CredentialKind{"claude-code": CredentialOAuth}}); err == nil {
|
||||
t.Fatal("Register() succeeded with a required OAuth credential and no store")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,10 +77,17 @@ type ProxyOptions struct {
|
||||
// under, the org whose OAuth token sets it uses, and its Kit's network
|
||||
// policy and credential requests.
|
||||
type SessionOptions struct {
|
||||
Run string
|
||||
Org string
|
||||
Network *spec.PhasedNetwork
|
||||
Run string
|
||||
Org string
|
||||
Network *spec.PhasedNetwork
|
||||
// Credentials is what the Kit's descriptor declares; Bound is how the run
|
||||
// actually carries them, each service the run credential names mapped to
|
||||
// the kind the org's binding gave it. Declared and bound are two separate
|
||||
// facts, and neither widens the other: a rule may only fire for a service
|
||||
// the run carries as that same kind, and a service the run carries not at
|
||||
// all resolves as unbound whichever rule asks.
|
||||
Credentials []spec.CredentialCapability
|
||||
Bound map[string]CredentialKind
|
||||
}
|
||||
|
||||
// Proxy is the egress proxy sandbox containers reach through their relay.
|
||||
@@ -120,7 +127,7 @@ func (p *Proxy) Register(opts SessionOptions) (*Session, error) {
|
||||
}
|
||||
s := &Session{
|
||||
token: hex.EncodeToString(raw), run: opts.Run, org: opts.Org,
|
||||
injections: compileInjections(opts.Credentials), oauth: compileOAuthRules(opts.Credentials),
|
||||
injections: compileInjections(opts.Credentials, opts.Bound), oauth: compileOAuthRules(opts.Credentials, opts.Bound),
|
||||
}
|
||||
for _, rule := range s.oauth {
|
||||
if rule.required && p.oauth == nil {
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
||||
)
|
||||
|
||||
// oauthCredentialFileKit is a Kit declaring a proxy-managed OAuth credential
|
||||
@@ -55,7 +57,7 @@ func findFile(files []spec.File, path string) (spec.File, bool) {
|
||||
func TestAssembleRendersTheOAuthCredentialFile(t *testing.T) {
|
||||
l, err := Assemble(credentialFilePlan(t), image, LaunchParams{
|
||||
Execution: "exec-42", ProxyToken: "tok", CAPath: "/etc/archie/ca.pem",
|
||||
Bound: []string{"claude-code"},
|
||||
Bound: map[string]egress.CredentialKind{"claude-code": egress.CredentialOAuth},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -98,7 +100,7 @@ func TestAssembleRendersTheStoredExpiry(t *testing.T) {
|
||||
expires := time.UnixMilli(1700000000123)
|
||||
l, err := Assemble(credentialFilePlan(t), image, LaunchParams{
|
||||
Execution: "exec-42", ProxyToken: "tok", CAPath: "/etc/archie/ca.pem",
|
||||
Bound: []string{"claude-code"}, OAuth: map[string]OAuthFacts{"claude-code": {ExpiresAt: expires}},
|
||||
Bound: map[string]egress.CredentialKind{"claude-code": egress.CredentialOAuth}, OAuth: map[string]OAuthFacts{"claude-code": {ExpiresAt: expires}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -143,7 +145,7 @@ func TestAssembleRendersATOMLCredentialFile(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("admit: %v", err)
|
||||
}
|
||||
l, err := Assemble(p, image, LaunchParams{Execution: "exec-42", Bound: []string{"example"}})
|
||||
l, err := Assemble(p, image, LaunchParams{Execution: "exec-42", Bound: map[string]egress.CredentialKind{"example": egress.CredentialOAuth}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -186,7 +188,7 @@ func TestAssembleRefusesAPlaceholderItCannotRender(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("admit: %v", err)
|
||||
}
|
||||
_, err = Assemble(p, image, LaunchParams{Execution: "exec-42", Bound: []string{"example"}})
|
||||
_, err = Assemble(p, image, LaunchParams{Execution: "exec-42", Bound: map[string]egress.CredentialKind{"example": egress.CredentialOAuth}})
|
||||
if err == nil || !strings.Contains(err.Error(), "{{.NotAPlaceholder}}") {
|
||||
t.Fatalf("Assemble = %v, want a refusal naming {{.NotAPlaceholder}}", err)
|
||||
}
|
||||
@@ -210,7 +212,7 @@ func TestAssembleRefusesAMissingSentinel(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("admit: %v", err)
|
||||
}
|
||||
_, err = Assemble(p, image, LaunchParams{Execution: "exec-42", Bound: []string{"example"}})
|
||||
_, err = Assemble(p, image, LaunchParams{Execution: "exec-42", Bound: map[string]egress.CredentialKind{"example": egress.CredentialOAuth}})
|
||||
if err == nil || !strings.Contains(err.Error(), "access-token sentinel") {
|
||||
t.Fatalf("Assemble = %v, want a refusal naming the missing access-token sentinel", err)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"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
|
||||
@@ -40,7 +42,7 @@ func renderClaudeCredentialFile(t *testing.T, facts OAuthFacts) map[string]any {
|
||||
t.Fatalf("admit: %v", err)
|
||||
}
|
||||
l, err := Assemble(p, image, LaunchParams{
|
||||
Execution: "exec-42", Bound: []string{"claude-code"},
|
||||
Execution: "exec-42", Bound: map[string]egress.CredentialKind{"claude-code": egress.CredentialOAuth},
|
||||
OAuth: map[string]OAuthFacts{"claude-code": facts},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -102,7 +104,7 @@ func TestAssembleRefusesWhenNoScopesWereCaptured(t *testing.T) {
|
||||
t.Fatalf("admit: %v", err)
|
||||
}
|
||||
_, err = Assemble(p, image, LaunchParams{
|
||||
Execution: "exec-42", Bound: []string{"claude-code"},
|
||||
Execution: "exec-42", Bound: map[string]egress.CredentialKind{"claude-code": egress.CredentialOAuth},
|
||||
OAuth: map[string]OAuthFacts{"claude-code": {}},
|
||||
})
|
||||
if err == nil {
|
||||
|
||||
@@ -59,8 +59,13 @@ type LaunchParams struct {
|
||||
Execution string
|
||||
ProxyToken string
|
||||
CAPath string
|
||||
// Bound lists the credential services the run credential carries.
|
||||
Bound []string
|
||||
// Bound keys each credential service this run carries to the kind the
|
||||
// org's binding gave it (egress.CredentialKind). A service absent from
|
||||
// this map is unbound: its mode variable says none, it renders no
|
||||
// credential file, and the proxy refuses (required) or skips (optional)
|
||||
// any request needing it. The map carries kinds, never values -- the real
|
||||
// secret stays on the host.
|
||||
Bound map[string]egress.CredentialKind
|
||||
// OAuth is the non-secret facts of each bound OAuth credential's stored
|
||||
// token set, keyed by service, that a Kit's credentialFile renders: the
|
||||
// granted scopes and the expiry. The tokens themselves never cross into
|
||||
@@ -115,7 +120,7 @@ func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) {
|
||||
proxyEnv := egress.ProxyEnv(params.ProxyToken, params.CAPath)
|
||||
runtimeVars := map[string]string{"WORKSPACE_DIR": WorkspaceDir}
|
||||
for _, c := range creds {
|
||||
runtimeVars[credentialModeVar(c.Service)] = credentialMode(c, slices.Contains(params.Bound, c.Service))
|
||||
runtimeVars[credentialModeVar(c.Service)] = credentialMode(params.Bound[c.Service])
|
||||
}
|
||||
|
||||
l := Launch{Harness: agentexec.HarnessSpec{
|
||||
@@ -157,14 +162,15 @@ func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) {
|
||||
return l, nil
|
||||
}
|
||||
|
||||
// credentialFiles renders the credential file of every bound OAuth credential
|
||||
// whose Kit declares one: the path and structure the Kit names, with the
|
||||
// stored token set's scopes and expiry. A service the run credential does not
|
||||
// carry produces no file.
|
||||
func credentialFiles(creds []spec.CredentialCapability, bound []string, oauth map[string]OAuthFacts) ([]spec.File, error) {
|
||||
// credentialFiles renders the credential file of every OAuth-bound
|
||||
// credential whose Kit declares one: the path and structure the Kit names,
|
||||
// with the stored token set's scopes and expiry. A service bound as an API
|
||||
// key renders none -- its Kit may carry the OAuth declaration too, but this
|
||||
// run has no token set to put in the file.
|
||||
func credentialFiles(creds []spec.CredentialCapability, bound map[string]egress.CredentialKind, oauth map[string]OAuthFacts) ([]spec.File, error) {
|
||||
var files []spec.File
|
||||
for _, c := range creds {
|
||||
if c.OAuth == nil || c.OAuth.CredentialFile == nil || !slices.Contains(bound, c.Service) {
|
||||
if c.OAuth == nil || c.OAuth.CredentialFile == nil || bound[c.Service] != egress.CredentialOAuth {
|
||||
continue
|
||||
}
|
||||
content, err := renderCredentialFile(c, oauth[c.Service])
|
||||
@@ -216,15 +222,17 @@ func credentialModeVar(service string) string {
|
||||
return "SBX_CRED_" + strings.ToUpper(strings.ReplaceAll(service, "-", "_")) + "_MODE"
|
||||
}
|
||||
|
||||
func credentialMode(c spec.CredentialCapability, bound bool) string {
|
||||
switch {
|
||||
case !bound:
|
||||
return "none"
|
||||
case c.APIKey != nil:
|
||||
return "apikey"
|
||||
default:
|
||||
return "oauth"
|
||||
// credentialModeNone is the mode variable's value for a service this run does
|
||||
// not carry at all.
|
||||
const credentialModeNone = "none"
|
||||
|
||||
// credentialMode is one service's SBX_CRED_*_MODE value: how this run carries
|
||||
// the credential, or none when it carries none at all.
|
||||
func credentialMode(kind egress.CredentialKind) string {
|
||||
if kind == "" {
|
||||
return credentialModeNone
|
||||
}
|
||||
return string(kind)
|
||||
}
|
||||
|
||||
// declared resolves a hook's declared variable names against the runtime
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
||||
)
|
||||
|
||||
const launchKit = `
|
||||
@@ -52,7 +54,7 @@ var image = ImageConfig{
|
||||
User: "agent",
|
||||
}
|
||||
|
||||
var params = LaunchParams{Execution: "exec-42", ProxyToken: "tok", CAPath: "/etc/archie/ca.pem", Bound: []string{"example"}}
|
||||
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)
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/kit"
|
||||
)
|
||||
|
||||
@@ -85,7 +86,7 @@ func TestCredentialFileNeverCarriesARealToken(t *testing.T) {
|
||||
ExpiresAt: expires, Scopes: ancestors,
|
||||
}}}
|
||||
p := credentialFilePlan(t)
|
||||
bound := []string{"claude-code"}
|
||||
bound := map[string]egress.CredentialKind{"claude-code": egress.CredentialOAuth}
|
||||
|
||||
facts, err := l.oauthFacts(context.Background(), "acme", planCredentials(t, p), bound)
|
||||
if err != nil {
|
||||
@@ -126,7 +127,7 @@ func TestCredentialFileNeverCarriesARealToken(t *testing.T) {
|
||||
func TestOAuthExpiriesFailsClosed(t *testing.T) {
|
||||
p := credentialFilePlan(t)
|
||||
creds := planCredentials(t, p)
|
||||
bound := []string{"claude-code"}
|
||||
bound := map[string]egress.CredentialKind{"claude-code": egress.CredentialOAuth}
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := (&Launcher{}).oauthFacts(ctx, "acme", creds, bound); err == nil {
|
||||
|
||||
@@ -139,15 +139,15 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
granted, bound := l.resolveCredentials(req, k.creds)
|
||||
facts, err := l.oauthFacts(ctx, req.Org, k.creds, bound)
|
||||
granted, kinds := l.resolveCredentials(req, k.creds)
|
||||
facts, err := l.oauthFacts(ctx, req.Org, k.creds, kinds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if l.Grants != nil {
|
||||
l.Grants.Grant(req.Execution, granted)
|
||||
}
|
||||
session, err := l.Proxy.Register(egress.SessionOptions{Run: req.Execution, Org: req.Org, Network: k.network, Credentials: k.creds})
|
||||
session, err := l.Proxy.Register(egress.SessionOptions{Run: req.Execution, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: kinds})
|
||||
if err != nil {
|
||||
if l.Grants != nil {
|
||||
l.Grants.RevokeGrant(req.Execution)
|
||||
@@ -155,7 +155,7 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
|
||||
return nil, err
|
||||
}
|
||||
run := &Run{network: "archie-kit-" + req.Execution, token: session.Token(), execution: req.Execution}
|
||||
launch, err := kit.Assemble(k.plan, k.img, kit.LaunchParams{Execution: req.Execution, ProxyToken: session.Token(), CAPath: caPath, Bound: bound, OAuth: facts})
|
||||
launch, err := kit.Assemble(k.plan, k.img, kit.LaunchParams{Execution: req.Execution, ProxyToken: session.Token(), CAPath: caPath, Bound: kinds, OAuth: facts})
|
||||
if err != nil {
|
||||
l.release(run)
|
||||
return nil, err
|
||||
@@ -191,21 +191,28 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
|
||||
// of what the Kit composition declares, what config.CredentialBinding
|
||||
// entries exist, and what the dispatching identity is granted and belongs
|
||||
// to -- and resolves each to a real value. granted is the run/service ->
|
||||
// value map for Grants.Grant; bound is the service-name list kit.Assemble
|
||||
// renders sentinel-mode env vars from. A service failing any part of the
|
||||
// intersection, or whose binding's secret does not resolve, is simply
|
||||
// absent from both: it stays unbound, exactly as if credential@1 named a
|
||||
// service nobody configured. An OAuth-managed service is granted with no
|
||||
// value: its tokens are the org's State Store secret, which the proxy reads
|
||||
// only for a granted run.
|
||||
func (l *Launcher) resolveCredentials(req Request, creds []spec.CredentialCapability) (granted map[string]string, bound []string) {
|
||||
// value map for Grants.Grant; kinds is the service -> bound-kind map
|
||||
// kit.Assemble renders the container's credential state from. A service
|
||||
// failing any part of the intersection, or whose binding's secret does not
|
||||
// resolve, is simply absent from both: it stays unbound, exactly as if
|
||||
// credential@1 named a service nobody configured.
|
||||
//
|
||||
// The binding decides the kind, not the Kit's declaration: the sandbox kit
|
||||
// spec lets one credential declare apiKey and oauth together, meaning
|
||||
// "whichever the host has bound" (docker/claude-code-kit declares both for
|
||||
// anthropic). A binding naming a secret is therefore this run's API key even
|
||||
// when the Kit also declares OAuth, and a binding naming none is the org's
|
||||
// stored OAuth token set, carried with no value because the proxy reads the
|
||||
// tokens itself.
|
||||
func (l *Launcher) resolveCredentials(req Request, creds []spec.CredentialCapability) (granted map[string]string, kinds map[string]egress.CredentialKind) {
|
||||
granted = map[string]string{}
|
||||
kinds = map[string]egress.CredentialKind{}
|
||||
if l.Config == nil {
|
||||
return granted, bound
|
||||
return granted, kinds
|
||||
}
|
||||
current := l.Config.Get().Containers.Credentials
|
||||
if len(current) == 0 {
|
||||
return granted, bound
|
||||
return granted, kinds
|
||||
}
|
||||
declared := make([]string, len(creds))
|
||||
oauth := map[string]bool{}
|
||||
@@ -215,9 +222,17 @@ func (l *Launcher) resolveCredentials(req Request, creds []spec.CredentialCapabi
|
||||
}
|
||||
bindings := config.ContainerConfig{Credentials: current}.BoundCredentials(req.Org, req.GrantedServices, declared)
|
||||
for service, binding := range bindings {
|
||||
if oauth[service] {
|
||||
if binding.Secret == (config.SecretRef{}) {
|
||||
// The binding names no secret of its own. Only a Kit declaring
|
||||
// OAuth gives the service a meaning then -- the org's captured
|
||||
// token set. Anything else has nothing to carry and stays
|
||||
// unbound, rather than resolving to an empty value the proxy
|
||||
// would present as a credential.
|
||||
if !oauth[service] {
|
||||
continue
|
||||
}
|
||||
granted[service] = ""
|
||||
bound = append(bound, service)
|
||||
kinds[service] = egress.CredentialOAuth
|
||||
continue
|
||||
}
|
||||
if l.Secrets == nil {
|
||||
@@ -228,19 +243,20 @@ func (l *Launcher) resolveCredentials(req Request, creds []spec.CredentialCapabi
|
||||
continue
|
||||
}
|
||||
granted[service] = value
|
||||
bound = append(bound, service)
|
||||
kinds[service] = egress.CredentialAPIKey
|
||||
}
|
||||
return granted, bound
|
||||
return granted, kinds
|
||||
}
|
||||
|
||||
// oauthFacts reads the stored token set's scopes and expiry for each bound
|
||||
// OAuth credential whose Kit renders a credential file. The store is read only
|
||||
// for a service the run credential carries, and only those facts leave here:
|
||||
// the tokens stay behind, so the renderer has no way to write one.
|
||||
func (l *Launcher) oauthFacts(ctx context.Context, org string, creds []spec.CredentialCapability, bound []string) (map[string]kit.OAuthFacts, error) {
|
||||
// oauthFacts reads the stored token set's scopes and expiry for each
|
||||
// OAuth-bound credential whose Kit renders a credential file. The store is
|
||||
// read only for a service this run bound as OAuth, and only those facts
|
||||
// leave here: the tokens stay behind, so the renderer has no way to write
|
||||
// one.
|
||||
func (l *Launcher) oauthFacts(ctx context.Context, org string, creds []spec.CredentialCapability, kinds map[string]egress.CredentialKind) (map[string]kit.OAuthFacts, error) {
|
||||
facts := map[string]kit.OAuthFacts{}
|
||||
for _, c := range creds {
|
||||
if c.OAuth == nil || c.OAuth.CredentialFile == nil || !egress.IsOAuthManaged(c) || !slices.Contains(bound, c.Service) {
|
||||
if c.OAuth == nil || c.OAuth.CredentialFile == nil || kinds[c.Service] != egress.CredentialOAuth {
|
||||
continue
|
||||
}
|
||||
if l.OAuth == nil {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
||||
)
|
||||
|
||||
var errSecretNotConfigured = errors.New("secret not configured")
|
||||
@@ -45,18 +46,18 @@ func TestResolveCredentialsReadsTheLiveConfig(t *testing.T) {
|
||||
req := Request{Org: "acme", GrantedServices: []string{"openai"}}
|
||||
creds := []spec.CredentialCapability{declaredCred("openai")}
|
||||
|
||||
granted, bound := l.resolveCredentials(req, creds)
|
||||
if len(granted) != 0 || len(bound) != 0 {
|
||||
t.Fatalf("resolveCredentials with no configured binding = (%v, %v), want both empty", granted, bound)
|
||||
granted, kinds := l.resolveCredentials(req, creds)
|
||||
if len(granted) != 0 || len(kinds) != 0 {
|
||||
t.Fatalf("resolveCredentials with no configured binding = (%v, %v), want both empty", granted, kinds)
|
||||
}
|
||||
|
||||
l.Config.Set(config.Config{Containers: config.ContainerConfig{
|
||||
Credentials: []config.CredentialBinding{{Service: "openai", Org: "acme", Secret: ref}},
|
||||
}})
|
||||
|
||||
granted, bound = l.resolveCredentials(req, creds)
|
||||
if granted["openai"] != "sk-live" || len(bound) != 1 || bound[0] != "openai" {
|
||||
t.Fatalf("resolveCredentials after the binding was added = (%v, %v), want openai granted", granted, bound)
|
||||
granted, kinds = l.resolveCredentials(req, creds)
|
||||
if granted["openai"] != "sk-live" || kinds["openai"] != egress.CredentialAPIKey {
|
||||
t.Fatalf("resolveCredentials after the binding was added = (%v, %v), want openai granted as an API key", granted, kinds)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,6 +137,96 @@ func TestResolveCredentialsIsAnIntersection(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveCredentialsTakesTheBoundKindFromTheBinding: the sandbox kit
|
||||
// spec lets one credential@1 declare apiKey and oauth together, meaning
|
||||
// "whichever the host has bound" (the reference docker/claude-code-kit and
|
||||
// codex workloads both do, for anthropic and openai respectively). The org's
|
||||
// binding -- not the Kit's declaration -- therefore decides which one a run
|
||||
// carries, so a binding that names a secret must resolve that secret even
|
||||
// when the Kit also declares OAuth, and a binding that names none must never
|
||||
// resolve to an empty value the proxy would then inject.
|
||||
func TestResolveCredentialsTakesTheBoundKindFromTheBinding(t *testing.T) {
|
||||
keyRef := config.SecretRef{Engine: "env", Key: "ANTHROPIC_API_KEY"}
|
||||
// The registry's documented zero-ref behaviour: resolves to "" with no
|
||||
// error, which is why "no value" cannot be told apart from "no secret"
|
||||
// by the resolved string alone.
|
||||
secrets := fakeSecretResolver{keyRef: "sk-ant-real", config.SecretRef{}: ""}
|
||||
// dual declares apiKey and oauth on one service, the spec's "whichever
|
||||
// the host has bound" shape.
|
||||
dual := func() spec.CredentialCapability {
|
||||
c := declaredOAuth("anthropic")
|
||||
c.APIKey = &spec.APIKey{Name: "ANTHROPIC_API_KEY", ProxyManaged: true}
|
||||
return c
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
bindings []config.CredentialBinding
|
||||
creds []spec.CredentialCapability
|
||||
service string
|
||||
wantValue string
|
||||
wantKind egress.CredentialKind
|
||||
}{
|
||||
{
|
||||
name: "an API-key binding resolves its value for a Kit that also declares OAuth",
|
||||
bindings: []config.CredentialBinding{{Service: "anthropic", Org: "acme", Secret: keyRef}},
|
||||
creds: []spec.CredentialCapability{dual()},
|
||||
service: "anthropic",
|
||||
wantValue: "sk-ant-real",
|
||||
wantKind: egress.CredentialAPIKey,
|
||||
},
|
||||
{
|
||||
name: "a binding with no secret of its own is the OAuth token set",
|
||||
bindings: []config.CredentialBinding{{Service: "anthropic", Org: "acme"}},
|
||||
creds: []spec.CredentialCapability{dual()},
|
||||
service: "anthropic",
|
||||
wantValue: "",
|
||||
wantKind: egress.CredentialOAuth,
|
||||
},
|
||||
{
|
||||
name: "an OAuth-only Kit service with no binding secret is the OAuth token set",
|
||||
bindings: []config.CredentialBinding{{Service: "oauth-only", Org: "acme"}},
|
||||
creds: []spec.CredentialCapability{declaredOAuth("oauth-only")},
|
||||
service: "oauth-only",
|
||||
wantValue: "",
|
||||
wantKind: egress.CredentialOAuth,
|
||||
},
|
||||
{
|
||||
name: "an API-key Kit service with no binding secret stays unbound, never empty",
|
||||
bindings: []config.CredentialBinding{{Service: "api-key-only", Org: "acme"}},
|
||||
creds: []spec.CredentialCapability{declaredCred("api-key-only")},
|
||||
service: "api-key-only",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
l := &Launcher{
|
||||
Config: config.NewHolder(config.Config{Containers: config.ContainerConfig{
|
||||
Credentials: tt.bindings,
|
||||
}}),
|
||||
Secrets: secrets,
|
||||
}
|
||||
granted, kinds := l.resolveCredentials(Request{Org: "acme", GrantedServices: []string{tt.service}}, tt.creds)
|
||||
got, ok := granted[tt.service]
|
||||
if tt.wantKind == "" {
|
||||
if ok {
|
||||
t.Fatalf("resolveCredentials()[%q] = %q, want no credential at all", tt.service, got)
|
||||
}
|
||||
if kind := kinds[tt.service]; kind != "" {
|
||||
t.Fatalf("resolveCredentials() kind[%q] = %q, want unbound", tt.service, kind)
|
||||
}
|
||||
return
|
||||
}
|
||||
if !ok || got != tt.wantValue {
|
||||
t.Fatalf("resolveCredentials()[%q] = %q (present %v), want %q", tt.service, got, ok, tt.wantValue)
|
||||
}
|
||||
if kind := kinds[tt.service]; kind != tt.wantKind {
|
||||
t.Fatalf("resolveCredentials() kind[%q] = %q, want %q", tt.service, kind, tt.wantKind)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The skills store is mounted read-only whatever mode a Kit asks for, and
|
||||
// not at all when the operator configured none.
|
||||
func TestSkillsBinds(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user