ReviewOS

also looking at this

stacks/drone

feat: own runtime firmware candidates

#49
Merged chris wants to merge codex/runtime-candidate-ownership into main
24 files +1245 -92
tests/fixtures/updates/qualify-preparation.tsadded+61-0
Changes to tests/fixtures/updates/qualify-preparation.ts
@@ -0,0 +1,61 @@
1import assert from 'node:assert/strict'
2import { constants } from 'node:fs'
3import { createHash } from 'node:crypto'
4import { chmod, copyFile, mkdir, readFile, writeFile } from 'node:fs/promises'
5import { join, resolve } from 'node:path'
6import { platform, repository, target } from './repository'
7
8async function hash(path: string) { return createHash('sha256').update(await readFile(path)).digest('hex') }
9
10/** Exercise actual copied standalone commands. Signed payloads are synthetic and are never executed. */
11export async function qualifyPortablePreparation(directory: string, source: string) {
12 await mkdir(directory, { mode: 0o700 })
13 const binary = join(directory, 'drone'), sourceSha256 = await hash(source)
14 await copyFile(source, binary, constants.COPYFILE_EXCL); await chmod(binary, 0o700)
15 assert.equal(await hash(binary), sourceSha256); assert.equal(await hash(source), sourceSha256)
16 const repo = join(directory, 'repo'), fixture = await repository(repo, { rotate: true, custom: { releaseSequence: 9 } })
17 const enrollment = join(directory, 'enrollment'), store = join(directory, 'store'), output = join(directory, 'candidate')
18 const results: { command: string, exit: number, stdout: string, stderr: string }[] = []
19 async function run(args: string[], expected = 0) {
20 const child = Bun.spawn([binary, ...args], { cwd: directory, stdin: 'ignore', stdout: 'pipe', stderr: 'pipe' })
21 const timer = setTimeout(() => child.kill('SIGKILL'), 20000)
22 try {
23 const [stdout, stderr, exit] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text(), child.exited])
24 results.push({ command: args[0]!, exit, stdout, stderr }); assert.equal(exit, expected)
25 if (expected) { assert.equal(stdout, ''); assert.equal(stderr, 'Runtime image preparation unavailable: invalid trust, clock, rejected image or output failure\n') }
26 return stdout ? JSON.parse(stdout) : undefined
27 }
28 finally { clearTimeout(timer) }
29 }
30 try {
31 const enrolled = await run(['updates:enroll', '--trusted-root', fixture.rootPath, '--output', enrollment, '--target', target, '--platform', platform, '--root-sha256', fixture.rootSha256, '--minimum-release-sequence', '7'])
32 const flags = ['--enrollment', enrollment, '--enrollment-sha256', enrolled.enrollmentSha256]
33 const created = await run(['updates:store-init', ...flags, '--output', store])
34 const pinned = [...flags, '--store-sha256', created.storeSha256]
35 const repositoryFlags = ['--repository', repo, '--trusted-system-clock-source', 'operator-managed-test-clock']
36 await run(['updates:prepare', store, ...pinned, '--repository', repo, '--output', join(directory, 'missing-clock')], 1)
37 const prepared = await run(['updates:prepare', store, ...pinned, ...repositoryFlags, '--output', output])
38 assert.equal(prepared.store.releaseFloor, 9); assert.equal(prepared.store.attempt.phase, 'verified')
39 assert.equal(prepared.candidate.sha256, fixture.imageHash); assert.equal(prepared.trustStatePersisted, true); assert.equal(prepared.physicalDeploymentAllowed, false)
40 const root = prepared.store.metadata.find((row: { role: string }) => row.role === 'root')
41 assert.equal(root.version, 2); assert.equal(prepared.candidate.trustedRootSha256, root.sha256)
42 assert.deepEqual(await run(['updates:inspect', output, '--target', target, '--platform', platform, '--root-sha256', root.sha256, '--minimum-release-sequence', '9']), prepared.candidate)
43 await writeFile(fixture.imagePath, Buffer.alloc(fixture.image.length, 0xff))
44 await run(['updates:prepare', store, ...pinned, ...repositoryFlags, '--output', join(directory, 'bad-image')], 1)
45 const stopped = await run(['updates:store', store, ...pinned])
46 assert.equal(stopped.releaseFloor, 9); assert.equal(stopped.attempt.phase, 'maintenance')
47 const recovered = await run(['updates:recover', store, ...pinned])
48 assert.equal(recovered.releaseFloor, 9); assert.equal(recovered.attempt.phase, 'recovered'); assert.equal(Object.hasOwn(recovered, 'candidate'), false); assert.equal(recovered.physicalDeploymentAllowed, false)
49 await writeFile(fixture.imagePath, fixture.image)
50 const fresh = await run(['updates:prepare', store, ...pinned, ...repositoryFlags, '--output', join(directory, 'fresh-candidate')])
51 assert.equal(fresh.candidate.sha256, fixture.imageHash); assert.equal(fresh.store.releaseFloor, 9); assert.equal(fresh.physicalDeploymentAllowed, false)
52 await run(['updates:prepare', store, ...flags, '--store-sha256', '0'.repeat(64), ...repositoryFlags, '--output', join(directory, 'wrong-pin')], 1)
53 assert.deepEqual(await run(['updates:store', store, ...pinned]), fresh.store)
54 }
55 finally { await writeFile(join(directory, 'qualification.json'), JSON.stringify({ evidence: 'copied-standalone-preparation-v1', sourceSha256, results, physicalDeploymentAllowed: false }, null, 2)) }
56 return { evidence: 'copied-standalone-preparation-v1', operations: results.length, sourceSha256, physicalDeploymentAllowed: false }
57}
58if (import.meta.main) {
59 if (process.argv.length !== 4) throw new Error('Use qualify-preparation.ts NEW_DIRECTORY COMPILED_BINARY')
60 console.log(JSON.stringify(await qualifyPortablePreparation(resolve(process.argv[2]!), resolve(process.argv[3]!))))
61}