Files
archie/internal/infrastructure/egress/credentials.go
T
sam 9d2cd7b5b0 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

140 lines
4.5 KiB
Go

package egress
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/docker/sandbox-kit-spec/v3/spec"
)
// Sentinel is the value a sandbox sees in place of a proxy-managed key, so
// 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.
var ErrUnbound = errors.New("egress: credential not bound")
// Resolver supplies a credential's real value for one run. An implementation
// returns only secrets the run's credential carries, so what a Kit declares
// is always narrowed by what the org granted the run's identity.
type Resolver interface {
Resolve(ctx context.Context, run, service string) (string, error)
}
// ResolverFunc adapts a function to Resolver.
type ResolverFunc func(ctx context.Context, run, service string) (string, error)
func (f ResolverFunc) Resolve(ctx context.Context, run, service string) (string, error) {
return f(ctx, run, service)
}
// injection is one compiled credential@1 apiKey inject rule.
type injection struct {
service string
required bool
runtime bool
domain pattern
header string
format string
basic bool
username string
}
// 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 || bound[c.Service] == CredentialOAuth {
continue
}
for _, rule := range c.APIKey.Inject {
format := rule.Format
if format == "" {
format = "%s"
}
out = append(out, injection{
service: c.Service,
required: c.Required,
runtime: c.Phase == "runtime",
domain: compilePattern(rule.Domain),
header: rule.Header,
format: format,
basic: strings.EqualFold(rule.Scheme, "basic"),
username: rule.Username,
})
}
}
return out
}
// inject presents the session's credentials on a request policy has already
// admitted. Only rules for this phase and this host apply, so a key is never
// sent anywhere its Kit did not name.
func (p *Proxy) inject(ctx context.Context, s *Session, r *http.Request, host string, port int) error {
atRuntime := s.atRun.Load()
for _, rule := range s.injections {
if rule.runtime != atRuntime || !rule.domain.matches(normalizeHost(host), port) {
continue
}
if p.resolver == nil {
if rule.required {
return fmt.Errorf("credential %q is required and no resolver is configured", rule.service)
}
continue
}
secret, err := p.resolver.Resolve(ctx, s.run, rule.service)
switch {
case errors.Is(err, ErrUnbound) && !rule.required:
continue
case errors.Is(err, ErrUnbound):
return fmt.Errorf("credential %q is required and not bound for this run", rule.service)
case err != nil:
return fmt.Errorf("resolve credential %q: %w", rule.service, err)
}
if rule.basic {
r.SetBasicAuth(rule.username, secret)
continue
}
r.Header.Set(rule.header, fmt.Sprintf(rule.format, secret))
}
return nil
}
// SentinelEnv is the environment a sandbox gets for its Kit's API keys: the
// sentinel under each named key. An inject-only key has no presence at all.
func SentinelEnv(creds []spec.CredentialCapability) []string {
var env []string
for _, c := range creds {
if c.APIKey != nil && c.APIKey.Name != "" {
env = append(env, c.APIKey.Name+"="+Sentinel)
}
}
return env
}