diff --git a/.agents/skills/archie-config-and-flags/SKILL.md b/.agents/skills/archie-config-and-flags/SKILL.md index 0abc8be07..8968acca3 100644 --- a/.agents/skills/archie-config-and-flags/SKILL.md +++ b/.agents/skills/archie-config-and-flags/SKILL.md @@ -176,6 +176,8 @@ looking at before reporting one: | `pull_policy` | Default `missing` when enabled. Pool code recognises `missing` and `always`; other strings silently skip pre-pull. | | `network` | Empty uses best-effort self-network detection. Repository overlay pins `archie-core_default`. | | `profiles` | Named agent execution environments (image or Kit); resolved per task dispatch (`ContainerConfig.Profile`). Own control-plane resource `agent-profiles` (`json:"-"` on the field so `container-runtime-policies` never carries it), `ApplyMode: live` -- a profile change applies without a restart, unlike the rest of `[containers]`. | +| `credentials` | Maps a Kit `credential@1` service to a secret (`config.CredentialBinding`). Own control-plane resource `credential-bindings`, `ApplyMode: live`, `json:"-"` on the field. `kitrun.Launcher` reads it fresh from the live `config.Holder` on every `Launch`, never a value captured at boot -- see `internal/infrastructure/kitrun/launcher.go`'s `resolveCredentials`. A binding alone grants nothing; see `org`/`granted_credentials` below. | +| `org` / `granted_credentials` (root and per-identity) | An identity's org and the `credentials` service names it may resolve. `daemon.runKitTask` reads both fresh per dispatch (`d.configFor(task)`) and passes them to `kitrun.Request`; `config.ContainerConfig.BoundCredentials` computes the declared (Kit) ∩ granted (identity) ∩ org-matched intersection -- neither declared nor granted widens the other. | ### Channel settings diff --git a/config.example.toml b/config.example.toml index 17f10e050..af9e2ef1f 100644 --- a/config.example.toml +++ b/config.example.toml @@ -34,6 +34,8 @@ label = "archie" bot_user = "vectis-labs-archie" # 0 switches the cap off; omit the key to take the built-in default (400). diff_cap_lines = 400 +# org = "acme" # empty means the default org (a single-operator install's one identity) +# granted_credentials = ["openai"] # credential@1 services the root identity may resolve (see [containers.credentials]) # how many operator-triggered retries a parked task gets # before it moves to dead (global default; set per-repo to @@ -75,6 +77,8 @@ token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" } # name = "personal" # required; namespaces logs/events/NATS subjects # bot_user = "your-bot" # required # forge = { type = "github", host = "https://github.com", token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN_PERSONAL" } } +# org = "acme" # empty inherits the shared org (empty = default) +# granted_credentials = ["openai"] # credential@1 services this identity may resolve (see [containers.credentials]) [dispatch] # Can be of "assignee", "label", or "either" @@ -450,6 +454,16 @@ target = "127.0.0.1:9090" # REQUIRED. The State Store gRPC address arch # [containers.profiles.claude] # kit = ["docker/claude-code-kit@sha256:"] # adapter = "claude-code" # claude-code, codex, pi, omp or copilot; empty runs without capture tools or usage +# +# Credential bindings map a Kit's credential@1 service name to a secret. A +# binding alone grants nothing -- an identity resolves it only when its own +# org matches and the service is in its granted_credentials (see +# [[identities]] above). Once the State Store holds it, edit bindings in the +# dashboard (Settings, Advanced); a stored set replaces the file's. +# [[containers.credentials]] +# service = "openai" +# org = "acme" # empty means the default org +# secret = {engine = "env", key = "OPENAI_API_KEY"} # At least one [[repos]] entry is required before archied will poll # anything -- it is deliberately left commented rather than shipped active: diff --git a/internal/app/archied/kit_launcher.go b/internal/app/archied/kit_launcher.go index a921af320..97af2885d 100644 --- a/internal/app/archied/kit_launcher.go +++ b/internal/app/archied/kit_launcher.go @@ -59,9 +59,11 @@ func (b *boot) setupKitLauncher(ctx context.Context) { log.Warn("kit harness runs disabled: egress relay", "err", err) return } - // No credential resolver yet (archie-core-egkf.11): a Kit's required - // credential is refused at the proxy until run credentials carry secrets. - proxy := egress.NewProxy(ca, egress.ProxyOptions{}) + // grants is the production Resolver: Launch/Release Grant/RevokeGrant it + // per run with exactly the declared-and-granted intersection + // (docs/prds/external-agent-harness.md, Credentials; archie-core-egkf.11). + grants := egress.NewGrantResolver() + proxy := egress.NewProxy(ca, egress.ProxyOptions{Resolver: grants}) srv := &http.Server{Handler: proxy, ReadHeaderTimeout: 30 * time.Second} go func() { if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) { @@ -76,6 +78,7 @@ func (b *boot) setupKitLauncher(ctx context.Context) { b.kitLauncher = &kitrun.Launcher{ Pool: pool, Fetch: fetcher, Proxy: proxy, Networks: networks, AgentBinary: agentBinary, CAFile: egress.CACertPath(caDir), + Config: b.cfgHolder, Secrets: b.secrets, Grants: grants, } log.Info("kit harness runs enabled", "proxy", ln.Addr().String()) } diff --git a/internal/app/archied/reload.go b/internal/app/archied/reload.go index 073d746a8..5dc1649f8 100644 --- a/internal/app/archied/reload.go +++ b/internal/app/archied/reload.go @@ -152,7 +152,10 @@ var reloadableFields = map[string]bool{ // Containers.Profile, called from pinTaskProfile) and is also its own // live-applying control-plane resource (AgentProfileKind), never captured at // container-pool construction -- docs/prds/external-agent-harness.md -// "Selection": a Kit profile applies without a restart. Everything else in +// "Selection": a Kit profile applies without a restart. Credentials is the +// same shape one layer down: kitrun.Launcher reads Config.Get().Containers. +// Credentials fresh on every Launch (CredentialBindingsKind), never a slice +// captured once when the launcher was built. Everything else in // Containers (Image, MaxConcurrency, MaxUptime, PullPolicy, Network) is // frozen in the startup-built container pool -- the dispatchers re-read // MaxConcurrency but the pool captures it at construction @@ -161,7 +164,7 @@ var reloadableFields = map[string]bool{ // (display/link building only); the forge client itself is startup-built, so // Type/Token/TokenEnv stay requires-restart. var reloadableSubFields = map[string]map[string]bool{ - "Containers": {"VolumeTTL": true, "Profiles": true}, + "Containers": {"VolumeTTL": true, "Profiles": true, "Credentials": true}, "Forge": {"Host": true}, // Policy is carried into TaskConfig by ForTask (config.go); MCPServers, // WebFetch and Minimax are not and stay requires-restart. diff --git a/internal/app/controlplane/credential_bindings.go b/internal/app/controlplane/credential_bindings.go new file mode 100644 index 000000000..4c9031519 --- /dev/null +++ b/internal/app/controlplane/credential_bindings.go @@ -0,0 +1,78 @@ +package controlplane + +import "github.com/samcharles93/archie-core/internal/config" + +// CredentialBindingsKind is the control-plane resource mapping a Kit +// credential@1 service name to an org secret +// (docs/prds/external-agent-harness.md, Credentials; docs/prds/orgs-and-access.md). +// It exists so a binding applies without a restart, the same reason +// AgentProfileKind is its own resource: [containers].credentials in +// config.toml is only its seed, and kitrun.Launcher reads the live +// control-plane document on every launch, never a value captured at boot. +const CredentialBindingsKind = "credential-bindings" + +// credentialBinding is one config.CredentialBinding as the document writes +// it: explicit snake_case json tags, following channel_settings.go / +// container_settings.go's precedent, never the internal config struct +// (which has no json tags and would fall back to Go field names). +type credentialBinding struct { + Service string `json:"service"` + Org string `json:"org,omitempty"` + Secret credentialValue `json:"secret"` +} + +// credentialValue is config.SecretRef as this document writes it. +type credentialValue struct { + Engine string `json:"engine,omitempty"` + Key string `json:"key,omitempty"` +} + +func credentialBindingsDefinition() Definition { + return Definition{ + Kind: CredentialBindingsKind, + Title: "Credential bindings", + ApplyMode: "live", + Document: []credentialBinding{}, + Seed: seedCredentialBindings, + Validate: validateCredentialBindings, + } +} + +func seedCredentialBindings(cfg config.Config) any { + return credentialBindingsDocument(cfg.Containers.Credentials) +} + +func credentialBindingsDocument(bindings []config.CredentialBinding) []credentialBinding { + if bindings == nil { + return nil + } + out := make([]credentialBinding, len(bindings)) + for i, b := range bindings { + out[i] = credentialBinding{Service: b.Service, Org: b.Org, Secret: credentialValue{Engine: b.Secret.Engine, Key: b.Secret.Key}} + } + return out +} + +// credentialBindingsSettings reverses credentialBindingsDocument, for the +// layering in runtime_config.go and for validation against +// config.ContainerConfig.ValidateCredentialBindings. +func credentialBindingsSettings(bindings []credentialBinding) []config.CredentialBinding { + if bindings == nil { + return nil + } + out := make([]config.CredentialBinding, len(bindings)) + for i, b := range bindings { + out[i] = config.CredentialBinding{Service: b.Service, Org: b.Org, Secret: config.SecretRef{Engine: b.Secret.Engine, Key: b.Secret.Key}} + } + return out +} + +func validateCredentialBindings(input []byte) error { + // The rules live in the config package and are shared with the file + // document's own validation (config.ContainerConfig.ValidateCredentialBindings): + // which bindings are valid cannot differ between the file that seeds this + // resource and the resource that replaces it. + return validateAs(input, func(bindings []credentialBinding) error { + return config.ContainerConfig{Credentials: credentialBindingsSettings(bindings)}.ValidateCredentialBindings() + }) +} diff --git a/internal/app/controlplane/definitions.go b/internal/app/controlplane/definitions.go index b01955342..dfdc8dce1 100644 --- a/internal/app/controlplane/definitions.go +++ b/internal/app/controlplane/definitions.go @@ -8,7 +8,7 @@ import ( // Schemas are not declared here: each Definition names its document type, and // Definition.Descriptor derives the JSON Schema from it (internal/app/controlplane/schema.go). func builtinDefinitions(steps workflow.StepRegistry) []Definition { - definitions := []Definition{workflowDefinition(), workflowDefinitionsDefinition(steps), personaDefinition(), scheduleDefinition(), agentProfileDefinition()} + definitions := []Definition{workflowDefinition(), workflowDefinitionsDefinition(steps), personaDefinition(), scheduleDefinition(), agentProfileDefinition(), credentialBindingsDefinition()} definitions = append(definitions, modelDefinitions()...) definitions = append(definitions, operationalDefinitions()...) return definitions diff --git a/internal/app/controlplane/runtime_config.go b/internal/app/controlplane/runtime_config.go index c90f07ed2..2abe0b8c4 100644 --- a/internal/app/controlplane/runtime_config.go +++ b/internal/app/controlplane/runtime_config.go @@ -235,6 +235,18 @@ func runtimeToolConfigFrom(ctx context.Context, reader controlplanerpc.ResourceR }); err != nil { return config.Config{}, nil, err } + // CredentialBindingsKind is likewise its own resource: a binding applies + // without a restart, and a stored value replaces the file's outright. + if err := layerResource(ctx, reader, versions, CredentialBindingsKind, func(value []byte) error { + var bindings []credentialBinding + if err := json.Unmarshal(value, &bindings); err != nil { + return err + } + out.Containers.Credentials = credentialBindingsSettings(bindings) + return nil + }); err != nil { + return config.Config{}, nil, err + } return out, versions, nil } diff --git a/internal/app/controlplane/runtime_config_test.go b/internal/app/controlplane/runtime_config_test.go index 91f16ef44..c05cd831d 100644 --- a/internal/app/controlplane/runtime_config_test.go +++ b/internal/app/controlplane/runtime_config_test.go @@ -60,6 +60,7 @@ func TestRuntimeConfigUsesDatabaseResourcesAndPreservesBootstrapOnlySecrets(t *t PluginSettingsKind: map[string]any{"plugin_dir": "/plugins", "module_dir": "/modules", "secret_engine_dir": "/secrets", "skills_dir": "/skills"}, ContainerRuntimePoliciesKind: map[string]any{"image": "archie:next", "pull_policy": "missing"}, AgentProfileKind: map[string]any{"net": map[string]any{"tools": []string{"whois"}}}, + CredentialBindingsKind: []map[string]any{{"service": "openai", "org": "acme", "secret": map[string]any{"engine": "env", "key": "OPENAI_KEY"}}}, }}) got, versions, err := client.RuntimeConfig(t.Context(), base) @@ -70,7 +71,7 @@ func TestRuntimeConfigUsesDatabaseResourcesAndPreservesBootstrapOnlySecrets(t *t // publishes is the one it actually layered in (archie-core-pskb). for _, kind := range []string{ ProviderSettingsKind, ModelRoleAssignmentsKind, RepositoryPoliciesKind, ChannelSettingsKind, - SchedulingPolicyKind, ToolSettingsKind, PluginSettingsKind, ContainerRuntimePoliciesKind, AgentProfileKind, + SchedulingPolicyKind, ToolSettingsKind, PluginSettingsKind, ContainerRuntimePoliciesKind, AgentProfileKind, CredentialBindingsKind, } { if versions[kind] != 2 { t.Errorf("versions[%s] = %d, want the 2 the store answered with", kind, versions[kind]) @@ -552,7 +553,7 @@ func TestRuntimeResourceKindsApplyLive(t *testing.T) { modes[definition.Kind] = definition.ApplyMode } for _, kind := range []string{ - ProviderSettingsKind, ModelRoleAssignmentsKind, RepositoryPoliciesKind, SchedulingPolicyKind, AgentProfileKind, + ProviderSettingsKind, ModelRoleAssignmentsKind, RepositoryPoliciesKind, SchedulingPolicyKind, AgentProfileKind, CredentialBindingsKind, } { if modes[kind] != "live" { t.Errorf("%s applies %q, want live: the daemon re-layers this kind on a watch", kind, modes[kind]) @@ -658,3 +659,42 @@ func TestRuntimeConfigLayersStoredProfiles(t *testing.T) { t.Fatalf("layering mutated the file document's profiles: %v", got) } } + +// A stored CredentialBindingsKind value replaces the file's bindings +// outright; no stored value at all leaves the file's in effect (the same +// shape TestRuntimeConfigLayersStoredProfiles pins for AgentProfileKind, for +// the same reason: a binding applies without a restart). +func TestRuntimeConfigLayersStoredCredentialBindings(t *testing.T) { + base := config.Config{Containers: config.ContainerConfig{Image: "agent:1", Credentials: []config.CredentialBinding{ + {Service: "file-only", Org: "acme"}, + }}} + + t.Run("a stored value replaces the file's", func(t *testing.T) { + client := NewRPCClient(&runtimeConfigClient{values: map[string]any{ + SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "dispatch": map[string]any{"trigger": "assignee"}}, + ContainerRuntimePoliciesKind: map[string]any{"image": "agent:2"}, + CredentialBindingsKind: []map[string]any{{"service": "openai", "org": "acme", "secret": map[string]any{"engine": "env", "key": "OPENAI_KEY"}}}, + }}) + got, _, err := client.RuntimeConfig(t.Context(), base) + if err != nil { + t.Fatalf("RuntimeConfig: %v", err) + } + if len(got.Containers.Credentials) != 1 || got.Containers.Credentials[0].Service != "openai" || got.Containers.Credentials[0].Secret.Key != "OPENAI_KEY" { + t.Fatalf("credentials = %+v, want the stored binding", got.Containers.Credentials) + } + }) + + t.Run("no stored value inherits the file's", func(t *testing.T) { + got, _, err := runtimeConfigFrom(t.Context(), absentReader{}, base) + if err != nil { + t.Fatalf("runtimeConfigFrom: %v", err) + } + if len(got.Containers.Credentials) != 1 || got.Containers.Credentials[0].Service != "file-only" { + t.Fatalf("credentials = %+v, want the file's binding", got.Containers.Credentials) + } + }) + + if got := base.Containers.Credentials; len(got) != 1 { + t.Fatalf("layering mutated the file document's credentials: %v", got) + } +} diff --git a/internal/app/controlplane/validators_agree_test.go b/internal/app/controlplane/validators_agree_test.go index c11f6374b..58ce9fcf8 100644 --- a/internal/app/controlplane/validators_agree_test.go +++ b/internal/app/controlplane/validators_agree_test.go @@ -122,6 +122,17 @@ func TestResourceValidatorsRejectWhatEffectiveValidationRejects(t *testing.T) { }, wantErr: true, }, + { + name: "credential binding with an empty service name", + kind: CredentialBindingsKind, + value: []credentialBinding{ + {Service: "", Org: "acme"}, + }, + mutate: func(cfg *config.Config) { + cfg.Containers.Credentials = []config.CredentialBinding{{Service: "", Org: "acme"}} + }, + wantErr: true, + }, { name: "container runtime policies without an image", kind: ContainerRuntimePoliciesKind, diff --git a/internal/config/config.go b/internal/config/config.go index 0d7683765..0d039fd16 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -458,6 +458,20 @@ type Config struct { // Label marks issues archie should pick up. Label string `toml:"label" yaml:"label"` BotUser string `toml:"bot_user" yaml:"bot_user"` + // Org is this identity's org (docs/prds/orgs-and-access.md). Empty means + // "default": the org a single-operator install's one identity belongs to. + // A credential binding whose Org does not match a run's identity never + // resolves for it, regardless of what the identity is granted. + Org string `toml:"org" yaml:"org"` + // GrantedCredentials names the credential@1 services this identity may + // use, by CredentialBinding.Service (docs/prds/external-agent-harness.md, + // Organisations: "a harness run ... acts as the workflow's identity"). + // Empty grants none -- a binding existing is not a grant, the same + // fail-closed default the proxy itself already applies to an unbound + // required credential. Declared (what a Kit's descriptor asks for) and + // granted (this list) are two separate facts; only their intersection + // resolves, and neither widens the other. + GrantedCredentials []string `toml:"granted_credentials" yaml:"granted_credentials"` // BotEmail is the git author email; defaults to the GitHub noreply // address for BotUser. BotEmail string `toml:"bot_email" yaml:"bot_email"` @@ -547,6 +561,13 @@ type IdentityConfig struct { // BotEmail is the git author email. Falls back to a forge-appropriate // default from BotUser when empty. BotEmail string `toml:"bot_email" yaml:"bot_email"` + // Org overrides the shared Org for this identity. Empty inherits it. + Org string `toml:"org" yaml:"org"` + // GrantedCredentials overrides the shared GrantedCredentials for this + // identity. Nil (absent) inherits it; a present, empty list grants none + // -- the same explicit-empty-means-off shape DiffCapLines uses, needed + // because a plain nil already means "not set" here. + GrantedCredentials *[]string `toml:"granted_credentials" yaml:"granted_credentials"` // DiffCapLines overrides the shared cap for this identity. Nil (absent) // inherits it; an explicit 0 switches the cap off for this identity only. DiffCapLines *int `toml:"diff_cap_lines" yaml:"diff_cap_lines"` @@ -851,6 +872,79 @@ type ContainerConfig struct { // AgentProfileKind, seeded from this field but validated and stored // (and reloaded live) separately -- see internal/app/controlplane. Profiles map[string]AgentProfile `toml:"profiles" yaml:"profiles" json:"-"` + // Credentials binds a Kit's credential@1 service names to org secrets + // (docs/prds/external-agent-harness.md, Credentials). A binding names + // where the value comes from; it is not itself a grant -- an identity + // resolves a bound service only when its own Org matches and the service + // is in its GrantedCredentials. + // Credentials is excluded from the control-plane JSON document + // (container-runtime-policies): it is its own resource, CredentialBindingsKind, + // seeded from this field but validated and stored (and reloaded live) + // separately -- see internal/app/controlplane. + Credentials []CredentialBinding `toml:"credentials" yaml:"credentials" json:"-"` +} + +// CredentialBinding maps one Kit credential@1 service name to an org secret. +// It carries no secret value itself, only where to resolve one from: the +// existing secret registry (env/file), not yet the encrypted org-secret +// store docs/prds/binding-secret-encryption.md describes -- that store is +// separate, still-open work this does not attempt. +type CredentialBinding struct { + // Service names the credential@1 service a Kit's descriptor declares + // (docker/sandbox-kit-spec spec.CredentialCapability.Service). + Service string `toml:"service" yaml:"service"` + // Org is the org this binding belongs to. Empty means the default org. + // A run whose identity's Org does not match never resolves this binding, + // however it is granted (docs/prds/orgs-and-access.md: "No record is + // shared between orgs"). + Org string `toml:"org" yaml:"org"` + // Secret is where the real value is resolved from. + Secret SecretRef `toml:"secret" yaml:"secret"` +} + +// BoundCredentials returns the credential bindings one run may actually use: +// every declared service (what a Kit's descriptor asks for) that is also +// granted (the run's identity's GrantedCredentials) and belongs to org (the +// run's identity's Org). This is the whole enforcement of "authority is an +// intersection": declared and granted are two independent facts, neither +// widens the other, and only a service present in all three -- org, granted, +// declared -- resolves. A service missing from any one is simply absent from +// the result; the caller (the egress resolver) treats that as unbound. +func (c ContainerConfig) BoundCredentials(org string, granted, declared []string) map[string]CredentialBinding { + grantedSet := make(map[string]bool, len(granted)) + for _, g := range granted { + grantedSet[g] = true + } + declaredSet := make(map[string]bool, len(declared)) + for _, d := range declared { + declaredSet[d] = true + } + out := make(map[string]CredentialBinding) + for _, b := range c.Credentials { + if b.Org != org || !grantedSet[b.Service] || !declaredSet[b.Service] { + continue + } + out[b.Service] = b + } + return out +} + +// ValidateCredentialBindings rejects a binding with an empty service name or +// duplicated (service, org) pair: two bindings resolving the same Kit +// credential in the same org is ambiguous, not a fallback chain. +func (c ContainerConfig) ValidateCredentialBindings() error { + seen := make(map[string]bool, len(c.Credentials)) + for _, b := range c.Credentials { + if strings.TrimSpace(b.Service) == "" { + return fmt.Errorf("containers.credentials: a service name must not be empty") + } + key := b.Org + "\x00" + b.Service + if seen[key] { + return fmt.Errorf("containers.credentials: service %q is bound more than once for org %q", b.Service, b.Org) + } + seen[key] = true + } + return nil } // AgentProfile is a named execution environment for an agent. Secrets and diff --git a/internal/config/credentials_test.go b/internal/config/credentials_test.go new file mode 100644 index 000000000..28a4b61f1 --- /dev/null +++ b/internal/config/credentials_test.go @@ -0,0 +1,115 @@ +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) + } + }) + } +} diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 0adee72e3..eb886ad29 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -2089,6 +2089,12 @@ func configForIdentity(root config.Config, identity config.IdentityConfig) confi if identity.DiffCapLines != nil { root.DiffCapLines = identity.DiffCapLines } + if identity.Org != "" { + root.Org = identity.Org + } + if identity.GrantedCredentials != nil { + root.GrantedCredentials = *identity.GrantedCredentials + } root.Forge = identity.Forge root.Dispatch = identity.Dispatch root.Models = identity.Models diff --git a/internal/daemon/kit_task.go b/internal/daemon/kit_task.go index 9b7bbc922..ebb494e73 100644 --- a/internal/daemon/kit_task.go +++ b/internal/daemon/kit_task.go @@ -61,13 +61,20 @@ func (d *Daemon) runKitTask(ctx context.Context, task *workflow.Task, repo confi park("kit workflow interface", err) return } + // Org and GrantedServices are the dispatching identity's own facts, read + // fresh here rather than cached anywhere: the same per-task + // config.Config d.configFor already resolves for every other identity + // override (Forge, Budgets, ...). + taskCfg := d.configFor(task) run, err := d.KitLauncher.Launch(ctx, kitrun.Request{ - Execution: fmt.Sprintf("task-%d", task.ID), - Kit: profile.Kit, - Adapter: profile.Adapter, - WorkDir: workDir, - WorkerEnv: env, - GateRetries: iface.Needs.GateRetries, + Execution: fmt.Sprintf("task-%d", task.ID), + Kit: profile.Kit, + Adapter: profile.Adapter, + WorkDir: workDir, + WorkerEnv: env, + GateRetries: iface.Needs.GateRetries, + Org: taskCfg.Org, + GrantedServices: taskCfg.GrantedCredentials, }) if err != nil { park("kit launch failed", err) diff --git a/internal/infrastructure/configuration/validate.go b/internal/infrastructure/configuration/validate.go index 593d03a7f..f170d5713 100644 --- a/internal/infrastructure/configuration/validate.go +++ b/internal/infrastructure/configuration/validate.go @@ -486,6 +486,9 @@ func validateContainers(cfg *config.Config) error { if err := cfg.Containers.ValidateProfiles(); err != nil { return fmt.Errorf("%w: %w", ErrInvalidInput, err) } + if err := cfg.Containers.ValidateCredentialBindings(); err != nil { + return fmt.Errorf("%w: %w", ErrInvalidInput, err) + } return nil } diff --git a/internal/infrastructure/egress/grants.go b/internal/infrastructure/egress/grants.go new file mode 100644 index 000000000..a7156ab8d --- /dev/null +++ b/internal/infrastructure/egress/grants.go @@ -0,0 +1,59 @@ +package egress + +import ( + "context" + "sync" +) + +// GrantResolver is the production Resolver: it holds, per active run, only +// the secrets a caller explicitly Grant-ed it -- the intersection a run +// credential carries (docs/prds/external-agent-harness.md, Credentials; +// docs/prds/orgs-and-access.md, "Agents: decided once, carried as a run +// credential"). It never derives a secret from anything else, so a service +// nobody granted, or granted to a different run, always answers ErrUnbound. +type GrantResolver struct { + mu sync.RWMutex + grants map[string]map[string]string +} + +// NewGrantResolver returns an empty resolver: no run may resolve anything +// until Grant is called for it. +func NewGrantResolver() *GrantResolver { + return &GrantResolver{grants: map[string]map[string]string{}} +} + +// Grant records exactly what one run may resolve, replacing whatever it held +// before -- a repeated Grant for the same run (a retry, or a narrower set) +// never merges with the last, since a service the caller dropped must not +// keep resolving from a stale entry. +func (g *GrantResolver) Grant(run string, secrets map[string]string) { + g.mu.Lock() + defer g.mu.Unlock() + g.grants[run] = secrets +} + +// RevokeGrant ends a run: nothing it was granted resolves after this call. +func (g *GrantResolver) RevokeGrant(run string) { + g.mu.Lock() + defer g.mu.Unlock() + delete(g.grants, run) +} + +// Resolve implements Resolver. A run with no Grant, or a service not among +// what it was granted, answers ErrUnbound -- the only error ambiguous enough +// for an optional credential to pass over (see Resolver's doc). +func (g *GrantResolver) Resolve(_ context.Context, run, service string) (string, error) { + g.mu.RLock() + defer g.mu.RUnlock() + secrets, ok := g.grants[run] + if !ok { + return "", ErrUnbound + } + v, ok := secrets[service] + if !ok { + return "", ErrUnbound + } + return v, nil +} + +var _ Resolver = (*GrantResolver)(nil) diff --git a/internal/infrastructure/egress/grants_test.go b/internal/infrastructure/egress/grants_test.go new file mode 100644 index 000000000..f51d15767 --- /dev/null +++ b/internal/infrastructure/egress/grants_test.go @@ -0,0 +1,72 @@ +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") + } +} diff --git a/internal/infrastructure/kitrun/launcher.go b/internal/infrastructure/kitrun/launcher.go index d3cd0c97a..b8b675641 100644 --- a/internal/infrastructure/kitrun/launcher.go +++ b/internal/infrastructure/kitrun/launcher.go @@ -15,11 +15,27 @@ import ( "github.com/docker/sandbox-kit-spec/v3/spec" "github.com/samcharles93/archie-core/internal/agentexec" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/container" "github.com/samcharles93/archie-core/internal/infrastructure/egress" "github.com/samcharles93/archie-core/internal/infrastructure/kit" ) +// SecretResolver resolves a credential binding's secret value. *secret.Registry +// satisfies it; the interface stays narrow so kitrun does not need the whole +// secret engine surface, and a test can fake it with no engine at all. +type SecretResolver interface { + Resolve(config.SecretRef) (string, error) +} + +// Grants is what Launch and Release tell the egress proxy's resolver about a +// run's credentials: exactly the intersection Launch computed, and nothing +// once the run ends. *egress.GrantResolver satisfies it. +type Grants interface { + Grant(run string, secrets map[string]string) + RevokeGrant(run string) +} + const ( agentPath = "/opt/archie/archie-agent" caPath = "/opt/archie/ca.pem" @@ -35,6 +51,18 @@ type Launcher struct { // Kit container: the worker's own binary and the egress CA. AgentBinary string CAFile string + // Config is read fresh on every Launch, never captured once at + // construction: credential bindings are their own live control-plane + // resource (controlplane.CredentialBindingsKind), so a binding added or + // changed after this daemon started must take effect on the very next + // dispatch, the same way an agent profile already does + // (docs/prds/external-agent-harness.md, "Selection": applies without a + // restart). A nil Secrets or Grants degrades every credential to unbound + // rather than panicking: a daemon with no Kit profile configured wires + // neither. + Config *config.Holder + Secrets SecretResolver + Grants Grants mu sync.Mutex started bool @@ -55,6 +83,15 @@ type Request struct { // a positive one, and Launch refuses it rather than starting a // container no gate failure could ever resume. GateRetries int + // Org and GrantedServices are the dispatching identity's own facts + // (config.Config.Org, config.Config.GrantedCredentials), carried here + // because kitrun holds no store or config of its own. Launch resolves a + // Kit credential only where these agree with a configured + // CredentialBinding and the Kit's own declared service -- the + // declared-and-granted intersection docs/prds/external-agent-harness.md + // and orgs-and-access.md both require; neither side widens the other. + Org string + GrantedServices []string } // Run is a started Kit task. @@ -63,9 +100,10 @@ type Run struct { Harness agentexec.HarnessSpec // Volumes outlive the run; remove them with RemoveVolumes once the // execution has ended. - Volumes []kit.Volume - network string - token string + Volumes []kit.Volume + network string + token string + execution string } // Launch composes the request's Kits and starts its container. @@ -95,20 +133,27 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) { if err != nil { return nil, err } + granted, bound := l.resolveCredentials(req, creds) + if l.Grants != nil { + l.Grants.Grant(req.Execution, granted) + } session, err := l.Proxy.Register(egress.SessionOptions{Run: req.Execution, Network: network, Credentials: creds}) if err != nil { + if l.Grants != nil { + l.Grants.RevokeGrant(req.Execution) + } return nil, err } - run := &Run{network: "archie-kit-" + req.Execution, token: session.Token()} - launch, err := kit.Assemble(plan, img, kit.LaunchParams{Execution: req.Execution, ProxyToken: session.Token(), CAPath: caPath}) + run := &Run{network: "archie-kit-" + req.Execution, token: session.Token(), execution: req.Execution} + launch, err := kit.Assemble(plan, img, kit.LaunchParams{Execution: req.Execution, ProxyToken: session.Token(), CAPath: caPath, Bound: bound}) if err != nil { - l.Proxy.Revoke(run.token) + l.release(run) return nil, err } launch.Harness.Adapter, launch.Harness.MCPConfig = req.Adapter, adapter.MCPConfig run.Harness, run.Volumes = launch.Harness, launch.Volumes if err := l.Networks.Create(ctx, run.network); err != nil { - l.Proxy.Revoke(run.token) + l.release(run) return nil, err } run.Container, err = l.Pool.AcquireKit(ctx, container.KitSpec{ @@ -126,12 +171,57 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) { InstallDone: session.EnterRuntime, }) if err != nil { - l.Proxy.Revoke(run.token) + l.release(run) return nil, errors.Join(err, l.Networks.Remove(context.WithoutCancel(ctx), run.network)) } return run, nil } +// resolveCredentials computes the run's granted secrets -- the intersection +// 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. +func (l *Launcher) resolveCredentials(req Request, creds []spec.CredentialCapability) (granted map[string]string, bound []string) { + granted = map[string]string{} + if l.Secrets == nil || l.Config == nil { + return granted, bound + } + current := l.Config.Get().Containers.Credentials + if len(current) == 0 { + return granted, bound + } + declared := make([]string, len(creds)) + for i, c := range creds { + declared[i] = c.Service + } + bindings := config.ContainerConfig{Credentials: current}.BoundCredentials(req.Org, req.GrantedServices, declared) + for service, binding := range bindings { + value, err := l.Secrets.Resolve(binding.Secret) + if err != nil { + continue + } + granted[service] = value + bound = append(bound, service) + } + return granted, bound +} + +// release undoes what Launch already did for run: the egress grant, then the +// session. Call sites past this point pass the network/container errors +// through their own cleanup; release only ever needs to run once per Launch +// failure, so it takes no error to join. +func (l *Launcher) release(run *Run) { + if l.Grants != nil { + l.Grants.RevokeGrant(run.execution) + } + l.Proxy.Revoke(run.token) +} + // compose assembles the profile's Kits and reads the workload image's // config, the first Kit being the workload. func (l *Launcher) compose(ctx context.Context, refs []string) (*kit.Plan, kit.ImageConfig, error) { @@ -157,11 +247,11 @@ func (l *Launcher) compose(ctx context.Context, refs []string) (*kit.Plan, kit.I return plan, img, nil } -// Release stops the run's container, then removes its network and ends -// its egress session. +// Release stops the run's container, then removes its network, revokes its +// credential grant and ends its egress session. func (l *Launcher) Release(ctx context.Context, run *Run) error { l.Pool.Release(ctx, run.Container) - l.Proxy.Revoke(run.token) + l.release(run) return l.Networks.Remove(context.WithoutCancel(ctx), run.network) } diff --git a/internal/infrastructure/kitrun/launcher_test.go b/internal/infrastructure/kitrun/launcher_test.go new file mode 100644 index 000000000..49486f17c --- /dev/null +++ b/internal/infrastructure/kitrun/launcher_test.go @@ -0,0 +1,118 @@ +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) + } + } + }) + } +}