Public: anyone can read this; the crew can change it.
A new pinned machine fetches only its pinned commit, not the whole repository… #16518
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16355-a-new-pinned-machine-fetches-only-its-pi → main 0 commits fork
6 files
changed +100
−9
src/cloud/machine.ts +2 −2
| @@ -139,8 +139,8 @@ ${hardenFiles()} | ||
| 139 | 139 | runcmd: |
| 140 | 140 | ${hardenEarly()} |
| 141 | 141 | - curl -fsSL https://bun.sh/install | BUN_INSTALL=${BUN_HOME} bash -s "bun-v${BUN_VERSION}" |
| 142 | - for try in 1 2 3 4 5; do git clone ${p.source} ${CHECKOUT} && break; rm -rf ${CHECKOUT}; sleep 15; done | |
| 143 | - git -C ${CHECKOUT} checkout --detach ${p.version} | |
| 142 | - for try in 1 2 3 4 5; do rm -rf ${CHECKOUT} && git init -q ${CHECKOUT} && git -C ${CHECKOUT} fetch -q --depth 1 ${p.source} ${p.version} && break; sleep 15; done | |
| 143 | - git -C ${CHECKOUT} checkout -q --detach FETCH_HEAD | |
| 144 | 144 | ${hardenLate()} |
| 145 | 145 | - npm install -g @anthropic-ai/claude-code || logger -t forge-boot crew not installed, the forge starts without it |
| 146 | 146 | - systemctl enable --now forge.service |
test/fetch-by-id.test.ts added +83
| @@ -0,0 +1,83 @@ | ||
| 1 | /** | |
| 2 | * A pinned machine fetches one commit, not the history (#16355): the served upload-pack answers a | |
| 3 | * depth-1 fetch of a commit by id, over protocol v0 and v2, and refuses an id the repository does | |
| 4 | * not hold. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { runGit } from "../src/git/env.ts"; | |
| 11 | import { initBareRepo, repoDir } from "../src/git/repos.ts"; | |
| 12 | import { advertiseRefs, isGitService, serviceRpc } from "../src/git/smart-http.ts"; | |
| 13 | import { unwrap } from "../src/kernel/result.ts"; | |
| 14 | import { gitClientEnv } from "./helpers/git-client.ts"; | |
| 15 | ||
| 16 | async function git(args: string[], cwd: string) { | |
| 17 | const proc = Bun.spawn(["git", ...args], { cwd, env: gitClientEnv(), stdout: "pipe", stderr: "pipe" }); | |
| 18 | const [out, err, code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]); | |
| 19 | return { code, out: out.trim(), err: err.trim() }; | |
| 20 | } | |
| 21 | ||
| 22 | let root: string; | |
| 23 | let work: string; | |
| 24 | let server: ReturnType<typeof Bun.serve>; | |
| 25 | let url: string; | |
| 26 | let first: string; | |
| 27 | let tip: string; | |
| 28 | const dir = () => repoDir(root, "ada", "arm"); | |
| 29 | ||
| 30 | beforeAll(async () => { | |
| 31 | root = await mkdtemp(join(tmpdir(), "forge-byid-")); | |
| 32 | work = await mkdtemp(join(tmpdir(), "forge-byid-work-")); | |
| 33 | unwrap(await initBareRepo(dir())); | |
| 34 | const seed = join(work, "seed"); | |
| 35 | await git(["init", "-q", "-b", "main", seed], work); | |
| 36 | for (const n of ["one", "two"]) { | |
| 37 | await Bun.write(join(seed, "f"), n); | |
| 38 | await git(["add", "."], seed); | |
| 39 | await git(["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "-m", n], seed); | |
| 40 | if (n === "one") first = (await git(["rev-parse", "HEAD"], seed)).out; | |
| 41 | } | |
| 42 | await git(["push", "-q", dir(), "main"], seed); | |
| 43 | tip = unwrap(await runGit(["rev-parse", "refs/heads/main"], { cwd: dir() })); | |
| 44 | server = Bun.serve({ | |
| 45 | port: 0, | |
| 46 | async fetch(request) { | |
| 47 | const u = new URL(request.url); | |
| 48 | if (u.pathname === "/ada/arm.git/info/refs") { | |
| 49 | const service = u.searchParams.get("service"); | |
| 50 | return isGitService(service) ? advertiseRefs(dir(), service, request) : new Response("no", { status: 400 }); | |
| 51 | } | |
| 52 | if (u.pathname === "/ada/arm.git/git-upload-pack") return serviceRpc(dir(), "git-upload-pack", request); | |
| 53 | return new Response("no", { status: 404 }); | |
| 54 | }, | |
| 55 | }); | |
| 56 | url = `http://127.0.0.1:${server.port}/ada/arm.git`; | |
| 57 | }); | |
| 58 | ||
| 59 | afterAll(async () => { | |
| 60 | server.stop(true); | |
| 61 | for (const d of [root, work]) await rm(d, { recursive: true, force: true }); | |
| 62 | }); | |
| 63 | ||
| 64 | for (const version of ["0", "2"]) { | |
| 65 | test(`protocol v${version}: a commit that is not a branch tip is fetched alone, depth 1`, async () => { | |
| 66 | const into = join(work, `into-${version}`); | |
| 67 | await git(["init", "-q", into], work); | |
| 68 | const run = await git(["-c", `protocol.version=${version}`, "fetch", "-q", "--depth", "1", url, first], into); | |
| 69 | expect(run.err).toBe(""); | |
| 70 | expect(run.code).toBe(0); | |
| 71 | expect((await git(["rev-parse", "FETCH_HEAD"], into)).out).toBe(first); | |
| 72 | expect((await git(["checkout", "-q", "--detach", "FETCH_HEAD"], into)).code).toBe(0); | |
| 73 | expect((await git(["rev-list", "--count", "HEAD"], into)).out).toBe("1"); | |
| 74 | expect(first).not.toBe(tip); | |
| 75 | }); | |
| 76 | ||
| 77 | test(`protocol v${version}: an id the repository does not hold is refused`, async () => { | |
| 78 | const into = join(work, `none-${version}`); | |
| 79 | await git(["init", "-q", into], work); | |
| 80 | const run = await git(["-c", `protocol.version=${version}`, "fetch", "-q", "--depth", "1", url, "1".repeat(40)], into); | |
| 81 | expect(run.code).not.toBe(0); | |
| 82 | }); | |
| 83 | } |
test/own-forge.test.ts +2 −2
| @@ -102,7 +102,7 @@ describe("the boot config", () => { | ||
| 102 | 102 | " - npm install -g @anthropic-ai/claude-code || logger", |
| 103 | 103 | ); |
| 104 | 104 | expect(install).toBeGreaterThan( |
| 105 | script.indexOf(`checkout --detach ${COMMIT}`), | |
| 105 | script.indexOf("checkout -q --detach FETCH_HEAD"), | |
| 106 | 106 | ); |
| 107 | 107 | expect(install).toBeLessThan( |
| 108 | 108 | script.indexOf("systemctl enable --now forge.service"), |
| @@ -420,7 +420,7 @@ describe("a deploy that names no code", () => { | ||
| 420 | 420 | source: UPSTREAM, |
| 421 | 421 | version: COMMIT, |
| 422 | 422 | }); |
| 423 | expect(userData).toContain(`git clone ${UPSTREAM} /opt/forge`); | |
| 423 | expect(userData).toContain(`fetch -q --depth 1 ${UPSTREAM} ${COMMIT}`); | |
| 424 | 424 | expect(userData).toContain(`FORGE_UPSTREAM=${UPSTREAM}`); |
| 425 | 425 | } finally { |
| 426 | 426 | if (was === undefined) delete process.env["FORGE_COMMIT"]; |
test/pinned-forge.test.ts +2 −2
| @@ -91,7 +91,7 @@ describe("pinned forge", () => { | ||
| 91 | 91 | const droplet = calls[2]!.body; |
| 92 | 92 | expect(droplet.volumes).toEqual(["vol-1"]); |
| 93 | 93 | expect(JSON.stringify(calls.map((c) => c.body))).not.toContain(TOKEN); |
| 94 | expect(droplet.user_data).toContain(`checkout --detach ${V1}`); | |
| 94 | expect(droplet.user_data).toContain(`--depth 1 ${SOURCE} ${V1}`); | |
| 95 | 95 | expect(droplet.user_data).toContain("FORGE_DATA=/mnt/forge_acme_data"); |
| 96 | 96 | const p = getPinned(app.db, "acme")!; |
| 97 | 97 | expect(p).toMatchObject({ state: "running", volume: "vol-1" }); |
| @@ -141,7 +141,7 @@ describe("pinned forge", () => { | ||
| 141 | 141 | expect.stringMatching(/^DELETE \/v2\/droplets\/\d+$/), "GET /v2/volumes/vol-1", "POST /v2/account/keys", "POST /v2/droplets", "DELETE /v2/account/keys/4242", |
| 142 | 142 | ]); |
| 143 | 143 | expect(calls[4]!.body.volumes).toEqual(["vol-1"]); |
| 144 | expect(calls[4]!.body.user_data).toContain(`checkout --detach ${V2}`); | |
| 144 | expect(calls[4]!.body.user_data).toContain(`--depth 1 ${SOURCE} ${V2}`); | |
| 145 | 145 | const [latest] = changesOf(app.db, getPinned(app.db, "acme")!.id); |
| 146 | 146 | expect(latest).toMatchObject({ kind: "update", fromVersion: V1, toVersion: V2, by: admin().id }); |
| 147 | 147 | expect(Date.parse(latest!.at)).toBeGreaterThan(0); |
test/pinned-harden.test.ts +9 −1
| @@ -75,12 +75,20 @@ describe.each(Object.entries(boots))("the %s machine", (_name, boot) => { | ||
| 75 | 75 | expect(doc.runcmd).toContain("chown -R forge:forge /opt/forge"); |
| 76 | 76 | // The checkout is made before it is handed over, and the service starts last. |
| 77 | 77 | const at = (s: string) => doc.runcmd.findIndex((c) => c.includes(s)); |
| 78 | expect(at("git clone")).toBeLessThan(at("chown -R forge:forge /opt/forge")); | |
| 78 | expect(at("fetch -q --depth 1")).toBeLessThan(at("chown -R forge:forge /opt/forge")); | |
| 79 | 79 | expect(at("chown -R forge:forge /opt/forge")).toBeLessThan( |
| 80 | 80 | at("systemctl enable --now forge.service"), |
| 81 | 81 | ); |
| 82 | 82 | }); |
| 83 | 83 | |
| 84 | test("the boot fetches the pinned commit alone, at depth 1, and checks out what it fetched", () => { | |
| 85 | const fetch = doc.runcmd.find((c) => c.includes("--depth 1"))!; | |
| 86 | expect(fetch).toContain("git init"); | |
| 87 | expect(fetch).toMatch(/fetch -q --depth 1 \S+ [0-9a-f]{7,40}/); | |
| 88 | expect(doc.runcmd.some((c) => c.includes("git clone"))).toBe(false); | |
| 89 | expect(doc.runcmd.some((c) => c.includes("checkout -q --detach FETCH_HEAD"))).toBe(true); | |
| 90 | }); | |
| 91 | ||
| 84 | 92 | test("the unit is locked down and keeps only the data directory and the forge's home writable", () => { |
| 85 | 93 | for (const line of [ |
| 86 | 94 | "NoNewPrivileges=yes", |
test/pinned-hetzner.test.ts +2 −2
| @@ -87,7 +87,7 @@ describe("hetzner target", () => { | ||
| 87 | 87 | expect(volume.location).toBe("nbg1"); |
| 88 | 88 | expect(server).toMatchObject({ server_type: "cx23", location: volume.location, volumes: [77], automount: true }); |
| 89 | 89 | expect(server.user_data).toContain("FORGE_DATA=/mnt/HC_Volume_77"); |
| 90 | expect(server.user_data).toContain(`checkout --detach ${V1}`); | |
| 90 | expect(server.user_data).toContain(`--depth 1 ${SOURCE} ${V1}`); | |
| 91 | 91 | expect(server.user_data).not.toContain(TOKEN); |
| 92 | 92 | expect(getPinned(app.db, "acme")).toMatchObject({ target: "hetzner", state: "running", volume: "77", remote: "501", datacenter: "nbg1", size: "cx23" }); |
| 93 | 93 | }); |
| @@ -109,7 +109,7 @@ describe("hetzner target", () => { | ||
| 109 | 109 | const made = calls.find((c) => c.method === "POST" && c.path === "/v1/servers")!; |
| 110 | 110 | expect(made.body).toMatchObject({ server_type: "cx23", location: "nbg1" }); |
| 111 | 111 | expect(made.body.volumes).toEqual([77]); |
| 112 | expect(made.body.user_data).toContain(`checkout --detach ${V2}`); | |
| 112 | expect(made.body.user_data).toContain(`--depth 1 ${SOURCE} ${V2}`); | |
| 113 | 113 | expect(getPinned(app.db, "acme")).toMatchObject({ volume: "77", remote: "502", version: V2 }); |
| 114 | 114 | }); |
| 115 | 115 |
new above
offline