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-candidates.tsadded+90-0
Changes to tests/fixtures/updates/qualify-candidates.ts
@@ -0,0 +1,90 @@
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/** Real copied commands, signed synthetic bytes and optional retained older binary. Never executes a candidate. */
11export async function qualifyPortableCandidates(directory: string, source: string, olderBinary?: 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 olderSha256 = olderBinary ? await hash(olderBinary) : null
17 const repo = join(directory, 'repo'), fixture = await repository(repo, { rotate: true, custom: { releaseSequence: 9 } })
18 const enrollment = join(directory, 'enrollment'), store = join(directory, 'store')
19 const results: { command: string, binarySha256: string, exit: number, stdout: string, stderr: string }[] = []
20 async function run(args: string[], expected = 0, executable = binary) {
21 const child = Bun.spawn([executable, ...args], { cwd: directory, stdin: 'ignore', stdout: 'pipe', stderr: 'pipe' })
22 const timer = setTimeout(() => child.kill('SIGKILL'), 20000)
23 try {
24 const [stdout, stderr, exit] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text(), child.exited])
25 results.push({ command: args[0]!, binarySha256: executable === binary ? sourceSha256 : olderSha256!, exit, stdout, stderr }); assert.equal(exit, expected)
26 if (expected) { assert.equal(stdout, ''); assert.equal(stderr, 'Runtime update store unavailable: invalid trust, clock, state or storage failure\n') }
27 return stdout ? JSON.parse(stdout) : undefined
28 }
29 finally { clearTimeout(timer) }
30 }
31 try {
32 const enrolled = await run(['updates:enroll', '--trusted-root', fixture.rootPath, '--output', enrollment, '--target', target, '--platform', platform, '--root-sha256', fixture.rootSha256, '--minimum-release-sequence', '7'])
33 const flags = ['--enrollment', enrollment, '--enrollment-sha256', enrolled.enrollmentSha256]
34 const created = await run(['updates:store-init', ...flags, '--output', store])
35 const pinned = [...flags, '--store-sha256', created.storeSha256]
36 const repositoryFlags = ['--repository', repo, '--trusted-system-clock-source', 'operator-managed-test-clock']
37 const initialHash = await hash(join(store, 'state.sqlite'))
38 const empty = await run(['updates:candidates', store, ...pinned])
39 assert.equal(empty.stateFormat, 1); assert.equal(empty.candidate, null)
40 assert.equal(await hash(join(store, 'state.sqlite')), initialHash)
41 if (olderBinary) assert.deepEqual(await run(['updates:store', store, ...pinned], 0, olderBinary), created)
42 await run(['updates:reserve', store, ...pinned, '--repository', repo], 1)
43 const prepared = await run(['updates:reserve', store, ...pinned, ...repositoryFlags])
44 assert.equal(prepared.stateFormat, 2); assert.equal(prepared.store.releaseFloor, 9)
45 assert.equal(prepared.store.attempt.phase, 'verified'); assert.equal(prepared.store.metadata.find((row: { role: string }) => row.role === 'root').version, 2)
46 assert.equal(prepared.candidate.phase, 'prepared'); assert.equal(prepared.candidate.integrity, 'recorded-bytes-match')
47 assert.equal(prepared.candidate.description.sha256, fixture.imageHash); assert.equal(prepared.candidate.currentTrustValidated, false); assert.equal(prepared.physicalDeploymentAllowed, false)
48 assert.deepEqual(await run(['updates:candidates', store, ...pinned]), prepared)
49 await run(['updates:reserve', store, ...pinned, ...repositoryFlags], 1)
50 await run(['updates:retire', store, ...pinned, '--candidate', '0'.repeat(32)], 1)
51 assert.deepEqual(await run(['updates:candidates', store, ...pinned]), prepared)
52 if (olderBinary) {
53 const stateHash = await hash(join(store, 'state.sqlite')), writerHash = await hash(join(store, 'writer.sqlite'))
54 await run(['updates:store', store, ...pinned], 1, olderBinary)
55 assert.equal(await hash(join(store, 'state.sqlite')), stateHash); assert.equal(await hash(join(store, 'writer.sqlite')), writerHash)
56 assert.equal(await hash(olderBinary), olderSha256)
57 }
58 await run(['updates:candidates', store, ...flags, '--store-sha256', '0'.repeat(64)], 1)
59 const original = await readFile(join(prepared.candidate.directory, 'stage.json'))
60 await writeFile(join(prepared.candidate.directory, 'image.bin'), Buffer.alloc(fixture.image.length, 0xff))
61 const damaged = await run(['updates:candidates', store, ...pinned])
62 assert.equal(damaged.candidate.integrity, 'mismatch'); assert.equal(damaged.candidate.currentTrustValidated, false)
63 await writeFile(join(prepared.candidate.directory, 'image.bin'), fixture.image)
64 const retired = await run(['updates:retire', store, ...pinned, '--candidate', prepared.candidate.id])
65 assert.equal(retired.candidate, null); assert.equal(retired.retainedCandidates, 1); assert.equal(retired.store.releaseFloor, 9)
66 assert.deepEqual(await readFile(join(prepared.candidate.directory, 'stage.json')), original)
67 await writeFile(fixture.imagePath, Buffer.alloc(fixture.image.length, 0xff))
68 await run(['updates:reserve', store, ...pinned, ...repositoryFlags], 1)
69 const stopped = await run(['updates:candidates', store, ...pinned])
70 assert.equal(stopped.candidate.phase, 'reserved'); assert.equal(stopped.candidate.integrity, 'incomplete')
71 assert.equal(stopped.store.releaseFloor, 9); assert.equal(stopped.store.attempt.phase, 'maintenance')
72 await run(['updates:recover', store, ...pinned])
73 const recovered = await run(['updates:candidates', store, ...pinned])
74 assert.deepEqual(recovered.candidate, stopped.candidate); assert.equal(recovered.store.releaseFloor, 9)
75 await run(['updates:retire', store, ...pinned, '--candidate', stopped.candidate.id])
76 await writeFile(fixture.imagePath, fixture.image)
77 const fresh = await run(['updates:reserve', store, ...pinned, ...repositoryFlags])
78 assert.equal(fresh.retainedCandidates, 3); assert.notEqual(fresh.candidate.id, prepared.candidate.id)
79 assert.equal(fresh.candidate.integrity, 'recorded-bytes-match'); assert.equal(fresh.store.releaseFloor, 9)
80 assert.equal(fresh.candidate.currentTrustValidated, false); assert.equal(fresh.physicalDeploymentAllowed, false)
81 assert.deepEqual(await readFile(join(prepared.candidate.directory, 'image.bin')), fixture.image)
82 assert.deepEqual(await readFile(join(prepared.candidate.directory, 'stage.json')), original)
83 }
84 finally { await writeFile(join(directory, 'qualification.json'), JSON.stringify({ evidence: 'copied-standalone-owned-candidates-v1', sourceSha256, olderSha256, results, physicalDeploymentAllowed: false }, null, 2)) }
85 return { evidence: 'copied-standalone-owned-candidates-v1', operations: results.length, sourceSha256, olderSha256, physicalDeploymentAllowed: false }
86}
87if (import.meta.main) {
88 if (process.argv.length !== 4 && process.argv.length !== 5) throw new Error('Use qualify-candidates.ts NEW_DIRECTORY COMPILED_BINARY [OLDER_BINARY]')
89 console.log(JSON.stringify(await qualifyPortableCandidates(resolve(process.argv[2]!), resolve(process.argv[3]!), process.argv[4] ? resolve(process.argv[4]) : undefined)))
90}