Files
archie/internal/infrastructure/kitrun/launcher_test.go
T
sam 58984c8bc1 feat(egress): credential resolver backed by declared-and-granted run credentials
docs/prds/external-agent-harness.md (Credentials) and orgs-and-access.md:
egress.Resolver had no production implementation, and no run carried a
credential naming the org secrets its identity is granted, so a Kit's
required credential@1 was refused at the proxy and no harness run
authenticated (archie-core-egkf.11).

Two things, as scoped (this does not attempt #1026's full run-credential
consolidation replacing WorktreeGrantIssuer/StateStoreGrantIssuer -- that
PRD item is real, separate, and still open; this is deliberately narrower):

1. A run credential naming granted secrets: config.CredentialBinding maps a
   Kit credential@1 service to a secret; config.Config/IdentityConfig gain
   Org and GrantedCredentials (a per-identity allowlist of service names,
   reusing the existing per-identity config-override pattern DiffCapLines
   already uses, not a new Cedar/Authorizer integration -- KindSecret policy
   objects exist in internal/domain/access but assembling a full Principal
   is #1026's job, not a two-line addition here). config.ContainerConfig.
   BoundCredentials computes the declared (Kit) ∩ granted (identity) ∩
   org-matched intersection Sam's own rule requires: neither side widens
   the other, proven by a mutation-checked test flipping each of the three
   independently.

2. egress.GrantResolver: the production Resolver, holding only what a
   caller explicitly Grant-ed one run, replaced wholesale (not merged) on
   a repeated Grant, gone entirely on RevokeGrant. kitrun.Launcher.Launch
   computes the intersection per launch and resolves real values through
   the existing secret registry (SecretResolver), Grants it before the
   proxy session opens, and Release/error-path rollback (a new shared
   Launcher.release) always revokes it.

Course-corrected mid-implementation (flagged by Sam): credential bindings
first landed as a boot-time config.ContainerConfig.Credentials field
captured once into Launcher.Bindings -- exactly the "parses but is not a
resource" trap CLAUDE.md warns about, and inconsistent with agent-profiles
(#1207) making a Kit profile apply without a restart. Rebuilt as its own
live control-plane resource, CredentialBindingsKind ("credential-bindings"),
following the agent-profiles/container-runtime-policies split precisely:
config.ContainerConfig.Credentials is json:"-", [containers].credentials
is only its seed, and kitrun.Launcher now holds *config.Holder (not a
captured slice) and reads Containers.Credentials fresh on every Launch.
Org/GrantedCredentials were already read fresh per dispatch (d.configFor
inside runKitTask, never cached), so no equivalent fix was needed there.

Deliberately not built: identities and grants as their own State Store
resource (orgs-and-access.md's "Org owns ... identities and their grants").
Identities are entirely config.toml-defined today with no control-plane
resource at all; moving GrantedCredentials alone into one would put half
an identity in two places. That migration is identities' own scope, not
credential bindings'.
2026-09-27 06:14:22 +10:00

119 lines
4.0 KiB
Go

package kitrun
import (
"errors"
"testing"
"github.com/docker/sandbox-kit-spec/v3/spec"
"github.com/samcharles93/archie-core/internal/config"
)
var errSecretNotConfigured = errors.New("secret not configured")
type fakeSecretResolver map[config.SecretRef]string
func (f fakeSecretResolver) Resolve(ref config.SecretRef) (string, error) {
v, ok := f[ref]
if !ok {
return "", errSecretNotConfigured
}
return v, nil
}
func declaredCred(service string) spec.CredentialCapability {
return spec.CredentialCapability{Service: service}
}
// TestResolveCredentialsReadsTheLiveConfig is the "reads current, not a boot
// snapshot" requirement: two calls against the same Launcher, differing only
// in what l.Config now holds, must produce different results. A Launcher
// that captured Bindings once at construction (the reverted design) cannot
// pass this.
func TestResolveCredentialsReadsTheLiveConfig(t *testing.T) {
ref := config.SecretRef{Engine: "env", Key: "OPENAI_KEY"}
l := &Launcher{
Config: config.NewHolder(config.Config{}),
Secrets: fakeSecretResolver{ref: "sk-live"},
}
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)
}
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)
}
}
// TestResolveCredentialsIsAnIntersection is the isolation proof at the
// Launcher boundary: a secret the identity is not granted, or a binding from
// another org, is never in the resolved set, however the Kit declares it.
func TestResolveCredentialsIsAnIntersection(t *testing.T) {
openaiRef := config.SecretRef{Engine: "env", Key: "OPENAI_KEY"}
otherRef := config.SecretRef{Engine: "env", Key: "OTHER_KEY"}
l := &Launcher{
Config: config.NewHolder(config.Config{Containers: config.ContainerConfig{
Credentials: []config.CredentialBinding{
{Service: "openai", Org: "acme", Secret: openaiRef},
{Service: "other-org-svc", Org: "other-org", Secret: otherRef},
},
}}),
Secrets: fakeSecretResolver{openaiRef: "sk-acme", otherRef: "sk-other"},
}
tests := []struct {
name string
req Request
creds []spec.CredentialCapability
want map[string]string
mutated bool
}{
{
name: "declared, granted, matching org resolves",
req: Request{Org: "acme", GrantedServices: []string{"openai"}},
creds: []spec.CredentialCapability{declaredCred("openai")},
want: map[string]string{"openai": "sk-acme"},
},
{
name: "not granted never resolves even though declared and the org matches",
req: Request{Org: "acme", GrantedServices: nil},
creds: []spec.CredentialCapability{declaredCred("openai")},
want: map[string]string{},
},
{
name: "a binding from another org never resolves even when granted",
req: Request{Org: "acme", GrantedServices: []string{"other-org-svc"}},
creds: []spec.CredentialCapability{declaredCred("other-org-svc")},
want: map[string]string{},
},
{
name: "not declared by the Kit never resolves even though granted",
req: Request{Org: "acme", GrantedServices: []string{"openai"}},
creds: nil,
want: map[string]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
granted, _ := l.resolveCredentials(tt.req, tt.creds)
if len(granted) != len(tt.want) {
t.Fatalf("resolveCredentials() = %v, want %v", granted, tt.want)
}
for service, value := range tt.want {
if granted[service] != value {
t.Fatalf("resolveCredentials()[%q] = %q, want %q", service, granted[service], value)
}
}
})
}
}