diff --git a/scripts/macos-runner-runtime-cache.mjs b/scripts/macos-runner-runtime-cache.mjs index 1c47342f18a..2d6af47dfad 100644 --- a/scripts/macos-runner-runtime-cache.mjs +++ b/scripts/macos-runner-runtime-cache.mjs @@ -12,12 +12,28 @@ export function macRuntimeInputsDigest(root) { return `sha256:${hash.digest("hex")}`; } -export function isTrustedMacRuntimeProducer(run, repository) { +const REQUIRED_VERIFIED_JOB_NAMES = [ + "macos_runner_runtime / build", + "macos_runner_runtime / verify_linux_controller", +]; + +export function isTrustedMacRuntimeWorkflowRun(run, repository) { return run?.repository?.full_name === repository && run.path === ".github/workflows/docker.yml" - && run.event === "push" && run.head_branch === "master" && run.conclusion === "success" + && run.event === "push" && run.head_branch === "master" && /^[a-f0-9]{40}$/.test(run.head_sha ?? ""); } +export function isTrustedMacRuntimeProducer(run, repository) { + return isTrustedMacRuntimeWorkflowRun(run, repository) && run.conclusion === "success"; +} + +export function hasVerifiedMacRuntimeJobs(jobs) { + if (!Array.isArray(jobs)) return false; + return REQUIRED_VERIFIED_JOB_NAMES.every(name => + jobs.some(job => job?.name === name && job.status === "completed" && job.conclusion === "success"), + ); +} + export async function findMacRuntimeArtifact({ digest, repository, token, fetchImpl = fetch }) { if (!token || repository !== "paperclipai/paperclip") return null; const name = `macos-runner-runtime-${digest.slice(7)}`; @@ -30,10 +46,13 @@ export async function findMacRuntimeArtifact({ digest, repository, token, fetchI return response.json(); }; const result = await get(`actions/artifacts?name=${name}&per_page=20`); - for (const artifact of (result.artifacts ?? []).filter(item => item.name === name && !item.expired).slice(0, 4)) { + for (const artifact of (result.artifacts ?? []).filter(item => item.name === name && !item.expired).slice(0, 10)) { if (!Number.isSafeInteger(artifact.workflow_run?.id) || !/^sha256:[a-f0-9]{64}$/.test(artifact.digest ?? "")) continue; const run = await get(`actions/runs/${artifact.workflow_run.id}`); - if (isTrustedMacRuntimeProducer(run, repository)) return { runId: run.id, name }; + if (!isTrustedMacRuntimeWorkflowRun(run, repository)) continue; + if (run.conclusion === "success") return { runId: run.id, name }; + const jobsResult = await get(`actions/runs/${artifact.workflow_run.id}/jobs?per_page=100`); + if (hasVerifiedMacRuntimeJobs(jobsResult?.jobs)) return { runId: run.id, name }; } return null; } diff --git a/scripts/macos-runner-runtime-cache.test.mjs b/scripts/macos-runner-runtime-cache.test.mjs index 270a7cf1bbc..fe1a97376be 100644 --- a/scripts/macos-runner-runtime-cache.test.mjs +++ b/scripts/macos-runner-runtime-cache.test.mjs @@ -1,15 +1,41 @@ import assert from "node:assert/strict"; import test from "node:test"; -import { findMacRuntimeArtifact, isTrustedMacRuntimeProducer } from "./macos-runner-runtime-cache.mjs"; +import { findMacRuntimeArtifact, hasVerifiedMacRuntimeJobs, isTrustedMacRuntimeProducer, isTrustedMacRuntimeWorkflowRun } from "./macos-runner-runtime-cache.mjs"; const repository = "paperclipai/paperclip", digest = `sha256:${"a".repeat(64)}`; const good = { id: 123, repository: { full_name: repository }, path: ".github/workflows/docker.yml", event: "push", head_branch: "master", conclusion: "success", head_sha: "b".repeat(40) }; +const verifiedJobs = [ + { name: "macos_runner_runtime / build", status: "completed", conclusion: "success" }, + { name: "macos_runner_runtime / verify_linux_controller", status: "completed", conclusion: "success" }, +]; -test("reuses only successful master image builds from the protected repository", () => { +test("reuses only trusted master image builds from the protected repository", () => { assert.equal(isTrustedMacRuntimeProducer(good, repository), true); + assert.equal(isTrustedMacRuntimeWorkflowRun({ ...good, conclusion: "cancelled" }, repository), true); for (const change of [{ repository: { full_name: "fork/project" } }, { event: "pull_request" }, { head_branch: "feature" }, { conclusion: "failure" }, { path: ".github/workflows/macos-runner-runtime.yml" }]) { assert.equal(isTrustedMacRuntimeProducer({ ...good, ...change }, repository), false); } + assert.equal(hasVerifiedMacRuntimeJobs(verifiedJobs), true); + assert.equal(hasVerifiedMacRuntimeJobs([verifiedJobs[0]]), false); + assert.equal(hasVerifiedMacRuntimeJobs([verifiedJobs[0], { ...verifiedJobs[1], conclusion: "cancelled" }]), false); + assert.equal(hasVerifiedMacRuntimeJobs([verifiedJobs[0], { ...verifiedJobs[1], status: "in_progress", conclusion: null }]), false); +}); + +test("reuses a verified artifact from a master docker.yml run cancelled downstream, but rejects unverified jobs", async () => { + const name = `macos-runner-runtime-${digest.slice(7)}`; + const records = [201, 202].map(id => ({ name, workflow_run: { id }, digest: `sha256:${"c".repeat(64)}` })); + const fetchImpl = async url => ({ + ok: true, + json: async () => { + if (url.includes("actions/artifacts")) return { artifacts: records }; + if (url.endsWith("/actions/runs/201")) return { ...good, id: 201, conclusion: "cancelled" }; + if (url.includes("/actions/runs/201/jobs")) return { jobs: [verifiedJobs[0], { ...verifiedJobs[1], conclusion: "cancelled" }] }; + if (url.endsWith("/actions/runs/202")) return { ...good, id: 202, conclusion: "cancelled" }; + if (url.includes("/actions/runs/202/jobs")) return { jobs: [...verifiedJobs, { name: "Watch exact-source verification", status: "completed", conclusion: "cancelled" }] }; + throw Error(`unexpected url: ${url}`); + }, + }); + assert.deepEqual(await findMacRuntimeArtifact({ digest, repository, token: "fixture", fetchImpl }), { runId: 202, name }); }); test("a matching artifact from a pull request cannot replace the master runtime", async () => {