diff --git a/internal/config/config.go b/internal/config/config.go index 43e775e09..f8a355fe3 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -9,6 +9,8 @@ import ( "net" "strings" "time" + + orgdomain "github.com/samcharles93/archie-core/internal/domain/org" ) // Duration is a time.Duration that unmarshals from TOML strings ("60s"). @@ -693,6 +695,7 @@ type CredentialBinding struct { // BoundCredentials returns the bindings a run may use: services that are // declared, granted, and belong to org. func (c ContainerConfig) BoundCredentials(org string, granted, declared []string) map[string]CredentialBinding { + org = credentialOrg(org) grantedSet := make(map[string]bool, len(granted)) for _, g := range granted { grantedSet[g] = true @@ -703,6 +706,7 @@ func (c ContainerConfig) BoundCredentials(org string, granted, declared []string } out := make(map[string]CredentialBinding) for _, b := range c.Credentials { + b.Org = credentialOrg(b.Org) if b.Org != org || !grantedSet[b.Service] || !declaredSet[b.Service] { continue } @@ -727,7 +731,14 @@ func (c Config) CredentialAccess(name string) (org string, granted []string) { granted = *id.GrantedCredentials } } - return org, granted + return credentialOrg(org), granted +} + +func credentialOrg(value string) string { + if value == "" { + return string(orgdomain.DefaultOrgID) + } + return value } // ValidateCredentialBindings rejects a binding with an empty service name or @@ -739,7 +750,7 @@ func (c ContainerConfig) ValidateCredentialBindings() error { if strings.TrimSpace(b.Service) == "" { return fmt.Errorf("containers.credentials: a service name must not be empty") } - key := b.Org + "\x00" + b.Service + key := credentialOrg(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) } diff --git a/internal/config/credential_org_test.go b/internal/config/credential_org_test.go new file mode 100644 index 000000000..a48aa3988 --- /dev/null +++ b/internal/config/credential_org_test.go @@ -0,0 +1,42 @@ +package config + +import ( + "testing" + + "github.com/samcharles93/archie-core/internal/domain/org" +) + +func TestCredentialOrgBoundary(t *testing.T) { + tests := []struct { + name, binding, requested, identity, root string + bound bool + want string + }{ + {name: "empty binding matches dashboard default", requested: string(org.DefaultOrgID), bound: true, want: string(org.DefaultOrgID)}, + {name: "explicit default matches root", binding: string(org.DefaultOrgID), bound: true, want: string(org.DefaultOrgID)}, + {name: "empty binding and root", bound: true, want: string(org.DefaultOrgID)}, + {name: "named tenant remains isolated", binding: "acme", requested: "other", identity: "acme", root: "other", want: "acme"}, + {name: "identity inherits root tenant", binding: "acme", requested: "acme", root: "acme", bound: true, want: "acme"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg := Config{Org: tt.root, GrantedCredentials: []string{"codex"}, Identities: []IdentityConfig{{Name: "bot", Org: tt.identity}}, Containers: ContainerConfig{Credentials: []CredentialBinding{{Service: "codex", Org: tt.binding}}}} + got, granted := cfg.CredentialAccess("bot") + if got != tt.want { + t.Fatalf("run org %q, want %q", got, tt.want) + } + bindings := cfg.Containers.BoundCredentials(tt.requested, granted, []string{"codex"}) + b, ok := bindings["codex"] + if ok != tt.bound { + t.Fatalf("bound=%v, want %v", ok, tt.bound) + } + if ok && b.Org == "" { + t.Fatal("returned binding retains empty org") + } + }) + } + cfg := ContainerConfig{Credentials: []CredentialBinding{{Service: "codex"}, {Service: "codex", Org: string(org.DefaultOrgID)}}} + if err := cfg.ValidateCredentialBindings(); err == nil { + t.Fatal("empty and explicit default bindings must be duplicates") + } +} diff --git a/internal/daemon/kit_task.go b/internal/daemon/kit_task.go index 6ee3ffd2f..aa965f641 100644 --- a/internal/daemon/kit_task.go +++ b/internal/daemon/kit_task.go @@ -66,6 +66,7 @@ func (d *Daemon) runKitTask(ctx context.Context, task *workflow.Task, repo confi // config.Config d.configFor already resolves for every other identity // override (Forge, Budgets, ...). taskCfg := d.configFor(task) + credentialOrg, grantedServices := taskCfg.CredentialAccess("") run, err := d.KitLauncher.Launch(ctx, kitrun.Request{ Execution: fmt.Sprintf("task-%d", task.ID), Kit: profile.Kit, @@ -73,8 +74,8 @@ func (d *Daemon) runKitTask(ctx context.Context, task *workflow.Task, repo confi WorkDir: workDir, WorkerEnv: env, GateRetries: iface.Needs().GateRetries, - Org: taskCfg.Org, - GrantedServices: taskCfg.GrantedCredentials, + Org: credentialOrg, + GrantedServices: grantedServices, RunCredential: credential, }) if err != nil {