|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { gitCredentialEnvsFor, type AgentDef } from "./agent-defs.js"; |
| 3 | +import { gitCredentialStore } from "./stores/git-credential-store.js"; |
| 4 | +import { resetKeyringForTests } from "./crypto/secret-box.js"; |
| 5 | +import type { Principal } from "./auth/principal.js"; |
| 6 | + |
| 7 | +// A valid 32-byte base64 key so credentialsKeyConfigured() returns true. |
| 8 | +const KEY = Buffer.alloc(32, 7).toString("base64"); |
| 9 | + |
| 10 | +function agent(over: Partial<AgentDef>): AgentDef { |
| 11 | + return { |
| 12 | + id: "a1", |
| 13 | + name: "Docs Agent", |
| 14 | + label: "Docs Agent", |
| 15 | + harness: "claude-agent-sdk", |
| 16 | + source: "github.com/acme/private-docs", |
| 17 | + ownerGroup: "acme", |
| 18 | + ...over, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +function resolveReturns(doc: { username?: string | null } | null, token = "ghp_secret") { |
| 23 | + return vi |
| 24 | + .spyOn(gitCredentialStore, "resolve") |
| 25 | + .mockResolvedValue(doc ? ({ doc: doc as never, token }) : null); |
| 26 | +} |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + process.env.AGENTOS_CREDENTIALS_KEY = KEY; |
| 30 | + resetKeyringForTests(); |
| 31 | +}); |
| 32 | +afterEach(() => { |
| 33 | + vi.restoreAllMocks(); |
| 34 | + delete process.env.AGENTOS_CREDENTIALS_KEY; |
| 35 | + resetKeyringForTests(); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("gitCredentialEnvsFor", () => { |
| 39 | + it("injects GIT_CONFIG_* extraHeader for a resolvable private https repo", async () => { |
| 40 | + const spy = resolveReturns({ username: "x-access-token" }, "ghp_abc"); |
| 41 | + const envs = await gitCredentialEnvsFor(agent({})); |
| 42 | + |
| 43 | + expect(spy).toHaveBeenCalledWith({ ownerGroups: ["acme"], host: "github.com" }); |
| 44 | + expect(envs.GIT_TERMINAL_PROMPT).toBe("0"); |
| 45 | + expect(envs.GIT_CONFIG_NOSYSTEM).toBe("1"); |
| 46 | + expect(envs.GIT_CONFIG_COUNT).toBe("1"); |
| 47 | + expect(envs.GIT_CONFIG_KEY_0).toBe("http.https://github.com/.extraHeader"); |
| 48 | + const expected = Buffer.from("x-access-token:ghp_abc").toString("base64"); |
| 49 | + expect(envs.GIT_CONFIG_VALUE_0).toBe(`Authorization: Basic ${expected}`); |
| 50 | + // The raw token never appears verbatim in any env value. |
| 51 | + expect(JSON.stringify(envs)).not.toContain("ghp_abc"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("defaults the basic-auth username to x-access-token when the cred has none", async () => { |
| 55 | + resolveReturns({ username: null }, "tok"); |
| 56 | + const envs = await gitCredentialEnvsFor(agent({})); |
| 57 | + const expected = Buffer.from("x-access-token:tok").toString("base64"); |
| 58 | + expect(envs.GIT_CONFIG_VALUE_0).toBe(`Authorization: Basic ${expected}`); |
| 59 | + }); |
| 60 | + |
| 61 | + it("honours a custom credential username", async () => { |
| 62 | + resolveReturns({ username: "oauth2" }, "tok"); |
| 63 | + const envs = await gitCredentialEnvsFor(agent({})); |
| 64 | + const expected = Buffer.from("oauth2:tok").toString("base64"); |
| 65 | + expect(envs.GIT_CONFIG_VALUE_0).toBe(`Authorization: Basic ${expected}`); |
| 66 | + }); |
| 67 | + |
| 68 | + it("scopes resolution to the agent's owner group", async () => { |
| 69 | + const spy = resolveReturns({ username: "x-access-token" }); |
| 70 | + await gitCredentialEnvsFor(agent({ ownerGroup: "team-x" })); |
| 71 | + expect(spy).toHaveBeenCalledWith({ ownerGroups: ["team-x"], host: "github.com" }); |
| 72 | + }); |
| 73 | + |
| 74 | + it("falls back to the actor's groups for a legacy unowned agent", async () => { |
| 75 | + const spy = resolveReturns({ username: "x-access-token" }); |
| 76 | + const actor = { id: "u1", groups: ["g1", "g2"] } as Principal; |
| 77 | + await gitCredentialEnvsFor(agent({ ownerGroup: null }), actor); |
| 78 | + expect(spy).toHaveBeenCalledWith({ ownerGroups: ["g1", "g2"], host: "github.com" }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("returns {} (unauthenticated) when no credential matches", async () => { |
| 82 | + resolveReturns(null); |
| 83 | + expect(await gitCredentialEnvsFor(agent({}))).toEqual({}); |
| 84 | + }); |
| 85 | + |
| 86 | + it("returns {} when the agent already carries a legacy gitToken", async () => { |
| 87 | + const spy = resolveReturns({ username: "x" }); |
| 88 | + expect(await gitCredentialEnvsFor(agent({ gitToken: "ghp_legacy" }))).toEqual({}); |
| 89 | + expect(spy).not.toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("returns {} when AGENTOS_CREDENTIALS_KEY is unset", async () => { |
| 93 | + delete process.env.AGENTOS_CREDENTIALS_KEY; |
| 94 | + resetKeyringForTests(); |
| 95 | + const spy = resolveReturns({ username: "x" }); |
| 96 | + expect(await gitCredentialEnvsFor(agent({}))).toEqual({}); |
| 97 | + expect(spy).not.toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("skips SSH sources (key auth, no http extraHeader)", async () => { |
| 101 | + const spy = resolveReturns({ username: "x" }); |
| 102 | + expect(await gitCredentialEnvsFor(agent({ source: "git@github.com:acme/private-docs.git" }))).toEqual({}); |
| 103 | + expect(spy).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("skips local-path sources", async () => { |
| 107 | + const spy = resolveReturns({ username: "x" }); |
| 108 | + expect(await gitCredentialEnvsFor(agent({ source: "/srv/agents/docs" }))).toEqual({}); |
| 109 | + expect(spy).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + it("returns {} when there is no owning group and no actor", async () => { |
| 113 | + const spy = resolveReturns({ username: "x" }); |
| 114 | + expect(await gitCredentialEnvsFor(agent({ ownerGroup: null }))).toEqual({}); |
| 115 | + expect(spy).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it("never throws — a store error degrades to an unauthenticated clone", async () => { |
| 119 | + vi.spyOn(gitCredentialStore, "resolve").mockRejectedValue(new Error("mongo down")); |
| 120 | + expect(await gitCredentialEnvsFor(agent({}))).toEqual({}); |
| 121 | + }); |
| 122 | + |
| 123 | + it("resolves an https:// URL form to the same host", async () => { |
| 124 | + const spy = resolveReturns({ username: "x-access-token" }); |
| 125 | + await gitCredentialEnvsFor(agent({ source: "https://github.com/acme/private-docs.git" })); |
| 126 | + expect(spy).toHaveBeenCalledWith({ ownerGroups: ["acme"], host: "github.com" }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments