188 lines
12 KiB
TypeScript
188 lines
12 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { createServer } from "node:http";
|
|
import { mkdtemp, mkdir, writeFile, readFile, rm } from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { promisify } from "node:util";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import { githubBrokerEnvironment, githubLauncherSource } from "./github-launcher.js";
|
|
import { prepareGitHubOperationLaunchers } from "./execution-target.js";
|
|
import { runChildProcess } from "./server-utils.js";
|
|
import type { CommandManagedRuntimeRunner } from "./command-managed-runtime.js";
|
|
const exec = promisify(execFile);
|
|
const cleanups: Array<() => Promise<unknown>> = [];
|
|
afterEach(async () => { for (const cleanup of cleanups.splice(0).reverse()) await cleanup(); });
|
|
|
|
describe("managed GitHub launchers", () => {
|
|
it("runs staged git and gh launchers inside an ES-module repository", async () => {
|
|
const root = await mkdtemp(path.join(os.tmpdir(), "paperclip-github-esm-"));
|
|
cleanups.push(() => rm(root, { recursive: true, force: true }));
|
|
const realBin = path.join(root, "real-bin");
|
|
await mkdir(realBin);
|
|
await writeFile(path.join(root, "package.json"), JSON.stringify({ type: "module" }));
|
|
await writeFile(path.join(realBin, "gh"), "#!/bin/sh\nprintf gh-fixture", { mode: 0o700 });
|
|
const target = { kind: "remote" as const, transport: "sandbox" as const,
|
|
providerKey: "fixture", remoteCwd: root,
|
|
runner: { execute: async (input: Parameters<CommandManagedRuntimeRunner["execute"]>[0]) => runChildProcess("github-esm-fixture", input.command, input.args ?? [], {
|
|
cwd: input.cwd ?? root, env: input.env ?? {}, stdin: input.stdin,
|
|
timeoutSec: 15, graceSec: 1, onLog: async () => {},
|
|
}) },
|
|
};
|
|
// The second run models the fresh per-run wrapper staged on warm startup.
|
|
for (const runId of ["cold", "warm"]) {
|
|
const env = await prepareGitHubOperationLaunchers({ runId, target, cwd: root,
|
|
env: { PATH: `${realBin}:${process.env.PATH}`, PAPERCLIP_GITHUB_BROKER_TOKEN: "" } });
|
|
const launcher = env.PAPERCLIP_GITHUB_LAUNCHER_DIR;
|
|
expect((await exec(path.join(launcher, "git"), ["--version"], { cwd: root, env: { ...process.env, ...env } })).stdout).toMatch(/^git version /);
|
|
expect((await exec(path.join(launcher, "gh"), [], { cwd: root, env: { ...process.env, ...env } })).stdout).toBe("gh-fixture");
|
|
}
|
|
});
|
|
|
|
it.each(["broker-offline", "config-unwritable", "capability-rejected"])("keeps real local Git usable when %s", async (failure) => {
|
|
const root = await mkdtemp(path.join(os.tmpdir(), "paperclip-github-failure-"));
|
|
cleanups.push(() => rm(root, { recursive: true, force: true }));
|
|
const bin = path.join(root, "managed");
|
|
await mkdir(bin);
|
|
await exec("git", ["init", root]);
|
|
await writeFile(path.join(bin, "git"), githubLauncherSource(), { mode: 0o700 });
|
|
const server = createServer((_req, res) => { res.writeHead(403); res.end(); });
|
|
await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve));
|
|
const { port } = server.address() as { port: number };
|
|
if (failure === "broker-offline") await new Promise<void>(resolve => server.close(() => resolve()));
|
|
else cleanups.push(() => new Promise<void>(resolve => server.close(() => resolve())));
|
|
const configRoot = path.join(root, "config");
|
|
if (failure === "config-unwritable") await writeFile(configRoot, "not a directory");
|
|
const result = await exec(path.join(bin, "git"), ["status", "--porcelain"], { cwd: root, env: {
|
|
...process.env, ...githubBrokerEnvironment({ GH_TOKEN: "host-must-not-leak" }, { url: `http://127.0.0.1:${port}`, token: "private-capability" }),
|
|
GH_CONFIG_DIR: configRoot, PATH: `${bin}:${process.env.PATH}`,
|
|
} });
|
|
expect(result.stderr).toContain(failure === "broker-offline" ? "broker_transport_unavailable" : failure === "config-unwritable" ? "configuration_directory_unavailable" : "capability_rejected");
|
|
expect(result.stderr).not.toMatch(/host-must-not-leak|private-capability/);
|
|
});
|
|
|
|
it("explains unavailable access while allowing local work without credentials", async () => {
|
|
const root = await mkdtemp(path.join(os.tmpdir(), "paperclip-github-diagnostic-"));
|
|
cleanups.push(() => rm(root, {recursive:true,force:true}));
|
|
const bin = path.join(root,"managed"), realBin = path.join(root,"real");
|
|
await mkdir(bin); await mkdir(realBin);
|
|
await writeFile(path.join(bin,"gh"), githubLauncherSource(), {mode:0o700});
|
|
await writeFile(path.join(realBin,"gh"), '#!/usr/bin/env node\nprocess.stdout.write(JSON.stringify({token:process.env.GH_TOKEN ?? null}));', {mode:0o700});
|
|
const server = createServer((_req,res) => {
|
|
res.setHeader("content-type","application/json");
|
|
res.end(JSON.stringify({status:"unavailable",reason:"More than one managed GitHub identity matches this run",env:{GH_TOKEN:"must-not-be-used"}}));
|
|
});
|
|
await new Promise<void>(resolve => server.listen(0,"127.0.0.1",resolve));
|
|
cleanups.push(() => new Promise<void>((resolve,reject) => server.close(error => error ? reject(error) : resolve())));
|
|
const {port} = server.address() as {port:number};
|
|
const result = await exec(path.join(bin,"gh"), [], {env:{...process.env,...githubBrokerEnvironment({GH_TOKEN:"host-token"},{url:`http://127.0.0.1:${port}`,token:"run-capability"}),PATH:`${bin}:${realBin}:${process.env.PATH}`}});
|
|
expect(JSON.parse(result.stdout)).toEqual({token:null});
|
|
expect(result.stderr).toContain("More than one managed GitHub identity matches this run");
|
|
expect(result.stderr).not.toMatch(/host-token|must-not-be-used|run-capability/);
|
|
});
|
|
it("captures each command's identity and clears host credentials when the next person has none", async () => {
|
|
const root = await mkdtemp(path.join(os.tmpdir(), "paperclip-github-launcher-test-"));
|
|
cleanups.push(() => rm(root, { recursive: true, force: true }));
|
|
const bin = path.join(root, "managed"), realBin = path.join(root, "real"), repo = path.join(root, "repo");
|
|
for (const dir of [bin, realBin, repo, path.join(bin, "gh-config")]) await mkdir(dir, { recursive: true });
|
|
for (const name of ["git", "gh"]) await writeFile(path.join(bin, name), githubLauncherSource(), { mode: 0o700 });
|
|
await writeFile(path.join(realBin, "gh"), `#!/usr/bin/env node
|
|
const {execFileSync}=require('node:child_process');
|
|
const identity=execFileSync('git',['var','GIT_AUTHOR_IDENT'],{encoding:'utf8'}).trim();
|
|
process.stdout.write(JSON.stringify({identity, token:process.env.GH_TOKEN ?? null, global:process.env.GIT_CONFIG_GLOBAL, config:process.env.GH_CONFIG_DIR}));
|
|
`, { mode: 0o700 });
|
|
let user: string | null = "A", captures = 0;
|
|
let heldCapture: (() => void) | null = null;
|
|
let releaseCapture: (() => void) | null = null;
|
|
const server = createServer((req, res) => {
|
|
captures++;
|
|
expect(req.headers.authorization).toBe("Bearer run-capability");
|
|
const selected = user;
|
|
res.setHeader("content-type", "application/json");
|
|
const finish = () => res.end(JSON.stringify(selected ? { status: "available", env: {
|
|
GH_TOKEN: `credential-${selected}`, GITHUB_TOKEN: `credential-${selected}`,
|
|
GIT_AUTHOR_NAME: selected, GIT_AUTHOR_EMAIL: `${selected}@example.test`,
|
|
GIT_COMMITTER_NAME: selected, GIT_COMMITTER_EMAIL: `${selected}@example.test`,
|
|
} } : { status: "absent", env: {} }));
|
|
if (heldCapture) { const captured = heldCapture; heldCapture = null; releaseCapture = finish; captured(); }
|
|
else finish();
|
|
});
|
|
await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve));
|
|
cleanups.push(() => new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve())));
|
|
const address = server.address() as { port: number };
|
|
const env: NodeJS.ProcessEnv = { ...process.env, ...githubBrokerEnvironment({
|
|
GH_TOKEN: "ambient-host-token", GIT_AUTHOR_NAME: "Host", GIT_AUTHOR_EMAIL: "host@example.test",
|
|
}, { url: `http://127.0.0.1:${address.port}`, token: "run-capability" }), PATH: `${bin}:${realBin}:${process.env.PATH}` };
|
|
const git = async (...args: string[]) => (await exec(path.join(bin, "git"), args, { cwd: repo, env })).stdout.trim();
|
|
await git("init");
|
|
await git("commit", "--allow-empty", "-m", "A");
|
|
user = "B";
|
|
await git("commit", "--allow-empty", "-m", "B");
|
|
user = "A";
|
|
await git("commit", "--allow-empty", "-m", "A again");
|
|
expect(await git("log", "--format=%an <%ae>|%cn <%ce>" )).toBe("A <A@example.test>|A <A@example.test>\nB <B@example.test>|B <B@example.test>\nA <A@example.test>|A <A@example.test>");
|
|
const before = captures;
|
|
const gh = JSON.parse((await exec(path.join(bin, "gh"), [], { cwd: repo, env })).stdout);
|
|
expect(gh.identity).toContain("A <A@example.test>");
|
|
expect(gh.token).toBe("credential-A");
|
|
expect(captures - before).toBe(1); // gh's child Git retains the same capture.
|
|
const captured = new Promise<void>(resolve => { heldCapture = resolve; });
|
|
const operationA = exec(path.join(bin, "gh"), [], { cwd: repo, env });
|
|
await captured;
|
|
user = "B";
|
|
const operationB = JSON.parse((await exec(path.join(bin, "gh"), [], { cwd: repo, env })).stdout);
|
|
releaseCapture!();
|
|
const completedA = JSON.parse((await operationA).stdout);
|
|
expect(completedA.token).toBe("credential-A");
|
|
expect(operationB.token).toBe("credential-B");
|
|
expect(completedA.config).not.toBe(operationB.config);
|
|
user = null;
|
|
await expect(git("var", "GIT_AUTHOR_IDENT")).rejects.toThrow();
|
|
expect(await git("status", "--porcelain")).toBe(""); // unrelated public/local Git still works
|
|
expect(env.GH_TOKEN).toBe("");
|
|
expect(env.GIT_AUTHOR_NAME).toBe("");
|
|
});
|
|
it("retries acquisition before spawning once, but never retries denied access or the Git command", async () => {
|
|
const root = await mkdtemp(path.join(os.tmpdir(), "paperclip-github-retry-"));
|
|
cleanups.push(() => rm(root, { recursive: true, force: true }));
|
|
const bin = path.join(root, "managed"), realBin = path.join(root, "real");
|
|
await mkdir(bin); await mkdir(realBin);
|
|
const countPath = path.join(root, "spawn-count");
|
|
await writeFile(path.join(bin, "gh"), githubLauncherSource(), { mode: 0o700 });
|
|
await writeFile(path.join(realBin, "gh"), `#!/usr/bin/env node
|
|
const fs = require('node:fs');
|
|
fs.appendFileSync(process.env.SPAWN_COUNT_PATH, 'spawn\\n');
|
|
process.stdout.write(process.env.GH_TOKEN || 'no-token');
|
|
process.exit(Number(process.env.CHILD_EXIT_CODE || '0'));
|
|
`, { mode: 0o700 });
|
|
let calls = 0, denied = false;
|
|
const server = createServer((req, res) => {
|
|
calls++;
|
|
expect(req.headers["x-paperclip-github-capability"]).toBe("fixture-capability");
|
|
if (denied) { res.writeHead(403); res.end("secret denial body"); return; }
|
|
if (calls === 1) { req.socket.destroy(); return; }
|
|
if (calls === 2) { res.writeHead(503); res.end("secret transient body"); return; }
|
|
res.setHeader("content-type", "application/json");
|
|
res.end(JSON.stringify({ status: "available", env: { GH_TOKEN: "current-fixture-token" } }));
|
|
});
|
|
await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve));
|
|
cleanups.push(() => new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve())));
|
|
const env = { ...process.env, ...githubBrokerEnvironment({}, {
|
|
url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, token: "fixture-capability",
|
|
}), PATH: `${bin}:${realBin}:${process.env.PATH}`, SPAWN_COUNT_PATH: countPath };
|
|
expect((await exec(path.join(bin, "gh"), [], { env })).stdout).toBe("current-fixture-token");
|
|
expect(calls).toBe(3);
|
|
expect(await readFile(countPath, "utf8")).toBe("spawn\n");
|
|
denied = true;
|
|
await expect(exec(path.join(bin, "gh"), [], { env })).rejects.toMatchObject({
|
|
code: 1, stderr: "Paperclip: GitHub credential context unavailable (denied); retry this operation.\n",
|
|
});
|
|
expect(calls).toBe(4);
|
|
expect(await readFile(countPath, "utf8")).toBe("spawn\n");
|
|
denied = false;
|
|
await expect(exec(path.join(bin, "gh"), [], { env: { ...env, CHILD_EXIT_CODE: "17" } })).rejects.toMatchObject({ code: 17 });
|
|
expect(calls).toBe(5);
|
|
expect(await readFile(countPath, "utf8")).toBe("spawn\nspawn\n");
|
|
});
|
|
|
|
});
|