Files
archie/internal/infrastructure/egress/grants_test.go
T
sam 416df69b00 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

73 lines
2.7 KiB
Go

package egress
import "testing"
// TestGrantResolverOnlyResolvesWhatWasGranted is the mutation-checked
// isolation proof: a service never granted for a run, a service granted only
// to a different run, and a service already revoked must all answer
// ErrUnbound -- there is no path from "asked" to "returned" that does not go
// through an explicit Grant.
func TestGrantResolverOnlyResolvesWhatWasGranted(t *testing.T) {
g := NewGrantResolver()
g.Grant("run-a", map[string]string{"openai": "sk-a"})
g.Grant("run-b", map[string]string{"anthropic": "sk-b"})
tests := []struct {
name string
run string
service string
want string
wantErr bool
}{
{name: "a granted service resolves", run: "run-a", service: "openai", want: "sk-a"},
{name: "a service granted to a different run never resolves here", run: "run-a", service: "anthropic", wantErr: true},
{name: "a service never granted to any run", run: "run-a", service: "unknown", wantErr: true},
{name: "an unknown run resolves nothing", run: "run-c", service: "openai", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := g.Resolve(t.Context(), tt.run, tt.service)
if tt.wantErr {
if err == nil {
t.Fatalf("Resolve(%q, %q) = (%q, nil), want ErrUnbound", tt.run, tt.service, got)
}
return
}
if err != nil || got != tt.want {
t.Fatalf("Resolve(%q, %q) = (%q, %v), want (%q, nil)", tt.run, tt.service, got, err, tt.want)
}
})
}
}
func TestGrantResolverRevokeRemovesEveryGrantForTheRun(t *testing.T) {
g := NewGrantResolver()
g.Grant("run-a", map[string]string{"openai": "sk-a", "anthropic": "sk-b"})
g.RevokeGrant("run-a")
if _, err := g.Resolve(t.Context(), "run-a", "openai"); err == nil {
t.Fatal("Resolve after RevokeGrant returned a value, want ErrUnbound")
}
if _, err := g.Resolve(t.Context(), "run-a", "anthropic"); err == nil {
t.Fatal("Resolve after RevokeGrant returned a value, want ErrUnbound")
}
}
// TestGrantResolverGrantReplacesNotMerges: a run re-granted with a narrower
// set (a Kit that stopped declaring a service it once did, or a retry
// dispatched after a grant changed) must lose the dropped service, not keep
// it from the previous Grant call.
func TestGrantResolverGrantReplacesNotMerges(t *testing.T) {
g := NewGrantResolver()
g.Grant("run-a", map[string]string{"openai": "sk-a", "anthropic": "sk-b"})
g.Grant("run-a", map[string]string{"openai": "sk-a2"})
got, err := g.Resolve(t.Context(), "run-a", "openai")
if err != nil || got != "sk-a2" {
t.Fatalf("Resolve(openai) = (%q, %v), want (sk-a2, nil)", got, err)
}
if _, err := g.Resolve(t.Context(), "run-a", "anthropic"); err == nil {
t.Fatal("anthropic still resolves after a narrower Grant replaced it, want ErrUnbound")
}
}