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'.
116 lines
3.4 KiB
Go
116 lines
3.4 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestBoundCredentialsIsAnIntersection is the mutation-checked isolation
|
|
// proof docs/prds/external-agent-harness.md and orgs-and-access.md require:
|
|
// declared (what a Kit's descriptor asks for) and granted (an identity's
|
|
// GrantedCredentials) are independent facts, and a binding's Org is a third.
|
|
// Flip any single one of the three and the service must fall out of the
|
|
// result -- never partially, never because another passed.
|
|
func TestBoundCredentialsIsAnIntersection(t *testing.T) {
|
|
c := ContainerConfig{Credentials: []CredentialBinding{
|
|
{Service: "openai", Org: "acme", Secret: SecretRef{Engine: "env", Key: "OPENAI_KEY"}},
|
|
{Service: "other-org-svc", Org: "other", Secret: SecretRef{Engine: "env", Key: "X"}},
|
|
}}
|
|
|
|
tests := []struct {
|
|
name string
|
|
org string
|
|
granted []string
|
|
declared []string
|
|
want []string
|
|
}{
|
|
{
|
|
name: "org, granted and declared all agree", org: "acme",
|
|
granted: []string{"openai"}, declared: []string{"openai"},
|
|
want: []string{"openai"},
|
|
},
|
|
{
|
|
name: "declared but not granted resolves nothing", org: "acme",
|
|
granted: nil, declared: []string{"openai"},
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "granted but not declared resolves nothing", org: "acme",
|
|
granted: []string{"openai"}, declared: nil,
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "granted and declared but the wrong org resolves nothing", org: "not-acme",
|
|
granted: []string{"openai"}, declared: []string{"openai"},
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "a service bound to a different org is never returned regardless of grants", org: "acme",
|
|
granted: []string{"other-org-svc"}, declared: []string{"other-org-svc"},
|
|
want: nil,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := c.BoundCredentials(tt.org, tt.granted, tt.declared)
|
|
var names []string
|
|
for name := range got {
|
|
names = append(names, name)
|
|
}
|
|
if len(names) != len(tt.want) {
|
|
t.Fatalf("BoundCredentials() = %v, want %v", names, tt.want)
|
|
}
|
|
for _, want := range tt.want {
|
|
if _, ok := got[want]; !ok {
|
|
t.Fatalf("BoundCredentials() = %v, want it to carry %q", names, want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateCredentialBindings(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
c ContainerConfig
|
|
wantErr string
|
|
}{
|
|
{name: "no bindings", c: ContainerConfig{}},
|
|
{name: "one binding", c: ContainerConfig{Credentials: []CredentialBinding{{Service: "openai", Org: "acme"}}}},
|
|
{
|
|
name: "empty service name",
|
|
c: ContainerConfig{Credentials: []CredentialBinding{{Service: "", Org: "acme"}}},
|
|
wantErr: "service name must not be empty",
|
|
},
|
|
{
|
|
name: "duplicate service in the same org",
|
|
c: ContainerConfig{Credentials: []CredentialBinding{
|
|
{Service: "openai", Org: "acme"},
|
|
{Service: "openai", Org: "acme"},
|
|
}},
|
|
wantErr: "bound more than once",
|
|
},
|
|
{
|
|
name: "same service, different orgs is not a duplicate",
|
|
c: ContainerConfig{Credentials: []CredentialBinding{
|
|
{Service: "openai", Org: "acme"},
|
|
{Service: "openai", Org: "other"},
|
|
}},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := tt.c.ValidateCredentialBindings()
|
|
if tt.wantErr == "" {
|
|
if err != nil {
|
|
t.Fatalf("ValidateCredentialBindings() = %v, want nil", err)
|
|
}
|
|
return
|
|
}
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Fatalf("ValidateCredentialBindings() = %v, want containing %q", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|