package kitrun import ( "os" "path/filepath" "strings" "testing" "github.com/docker/sandbox-kit-spec/v3/spec" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/egress" "github.com/samcharles93/archie-core/internal/infrastructure/kit" "github.com/samcharles93/archie-core/internal/skill" ) // A setup session has no task and no identity grant, so it must bind only the // org's own bindings for the Kit's declared services. Widening that // intersection would let one org's terminal carry another org's credential, // or a service the Kit never declared. func TestOrgCredentialKinds(t *testing.T) { t.Parallel() bindings := []config.CredentialBinding{ {Service: "github", Org: "acme", Secret: config.SecretRef{Engine: "builtin", Key: "gh"}}, {Service: "linear", Org: "acme"}, {Service: "github", Org: "other", Secret: config.SecretRef{Engine: "builtin", Key: "gh"}}, } creds := []spec.CredentialCapability{{Service: "github"}, {Service: "linear"}, {Service: "unbound"}} cases := []struct { name string org string want map[string]egress.CredentialKind services []string }{ { name: "binds the org's own declared services", org: "acme", want: map[string]egress.CredentialKind{ "github": egress.CredentialAPIKey, "linear": egress.CredentialOAuth, }, services: []string{"github", "linear"}, }, { name: "another org's binding never resolves", org: "other", want: map[string]egress.CredentialKind{"github": egress.CredentialAPIKey}, services: []string{ "github", }, }, { name: "unknown org binds nothing", org: "nobody", want: map[string]egress.CredentialKind{}, services: []string{}, }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { t.Parallel() got, services := orgCredentialKinds(bindings, tc.org, creds) if len(got) != len(tc.want) { t.Fatalf("kinds = %v, want %v", got, tc.want) } for service, kind := range tc.want { if got[service] != kind { t.Errorf("kind[%s] = %q, want %q", service, got[service], kind) } } if len(services) != len(tc.services) { t.Fatalf("services = %v, want %v", services, tc.services) } }) } } func TestSharedSkillsDoNotMaskImageDiscovery(t *testing.T) { t.Parallel() root := t.TempDir() store := skill.StoreDir(root) if err := os.MkdirAll(filepath.Join(store, "shared"), 0o700); err != nil { t.Fatal(err) } if err := os.Symlink("/etc", filepath.Join(store, "escape")); err != nil { t.Fatal(err) } for _, tc := range []struct { name, root string want int }{ {"shared", root, 1}, {"disabled", "", 0}, {"missing", filepath.Join(root, "missing"), 0}, } { t.Run(tc.name, func(t *testing.T) { binds := skillsBinds([]spec.AgentSkillsCapability{{Path: "/agent/skills"}}, tc.root) if len(binds) != tc.want { t.Fatalf("binds=%v", binds) } if len(binds) > 0 && binds[0] != filepath.Join(store, "shared")+":/agent/skills/shared:ro,z" { t.Fatalf("discovery directory masked or writable: %v", binds) } }) } } func TestHarnessProxyPasswordDoesNotExposeRunGrant(t *testing.T) { l := &Launcher{Proxy: egress.NewProxy(nil, egress.ProxyOptions{})} req := Request{RunCredential: "worker-state-store-grant", Org: "acme"} session, err := l.taskSession(req, kitNeeds{}, nil) if err != nil { t.Fatal(err) } defer l.Proxy.Revoke(t.Context(), session.Token()) if session.Token() == req.RunCredential { t.Fatal("harness proxy password exposes the worker's State Store grant") } launch, err := kit.Assemble(&kit.Plan{}, kit.ImageConfig{}, kit.LaunchParams{ProxyToken: session.Token()}) if err != nil { t.Fatal(err) } env := strings.Join(launch.Harness.Env, "\n") if strings.Contains(env, req.RunCredential) { t.Fatal("worker grant entered the harness environment") } if !strings.Contains(env, session.Token()) { t.Fatal("harness has no proxy password") } }