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/unit/updates-preparation.test.tsadded+163-0
Changes to tests/unit/updates-preparation.test.ts
@@ -0,0 +1,163 @@
1import { afterEach, expect, test } from 'bun:test'
2import { mkdir, mkdtemp, readFile, rename, rm, stat, writeFile } from 'node:fs/promises'
3import { tmpdir } from 'node:os'
4import { join } from 'node:path'
5import { enrollRuntimeTrust } from '../../resources/functions/drone/updates/enrollment'
6import { inspectStagedRuntimeFirmware } from '../../resources/functions/drone/updates/staging'
7import { createRuntimeTrustStore, inspectRuntimeTrustStore, prepareRuntimeFirmwareUpdate, recoverRuntimeTrustStore, refreshRuntimeTrustStore } from '../../resources/functions/drone/updates/store'
8import { platform, repository, target } from '../fixtures/updates/repository'
9import { qualifyPreparationInterruptions } from '../fixtures/updates/store-interruptions'
10import { epochRepository } from '../fixtures/updates/epochs'
11
12const roots: string[] = []
13afterEach(async () => { for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true }) })
14const clock = { source: 'test-independent-clock', read: () => new Date('2030-01-01T00:00:00Z') }
15async function setup(options: Parameters<typeof repository>[1] = {}) {
16 const root = await mkdtemp(join(tmpdir(), 'drone-prepare-update-')); roots.push(root)
17 const source = join(root, 'repo'), fixture = await repository(source, { ...options, custom: { releaseSequence: 9, ...options.custom } })
18 const enrollmentDirectory = join(root, 'enrollment')
19 const enrolled = enrollRuntimeTrust({ target, platform, trustedRootSha256: fixture.rootSha256, minimumReleaseSequence: 7, trustedRoot: fixture.rootPath, output: enrollmentDirectory })
20 const created = await createRuntimeTrustStore({ enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, output: join(root, 'store') })
21 return { root, source, fixture, reference: { enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, directory: join(root, 'store'), storeSha256: created.storeSha256 }, output: join(root, 'candidate') }
22}
23
24test('protected preparation authenticates actual image bytes and writes an independently verifiable signed candidate', async () => {
25 const item = await setup()
26 const result = await prepareRuntimeFirmwareUpdate({ ...item.reference, repository: item.source, output: item.output, clock })
27 expect(result.schema).toBe('drone-runtime-prepared-image-v1')
28 expect(result.store.generation).toBe(3)
29 expect(result.store.releaseFloor).toBe(9)
30 expect(result.store.attempt?.phase).toBe('verified')
31 expect(result.candidate.sha256).toBe(item.fixture.imageHash)
32 expect(result.candidate.length).toBe(item.fixture.image.length)
33 expect(result.trustStatePersisted).toBe(true)
34 expect(result.physicalDeploymentAllowed).toBe(false)
35 expect(result.candidate.trustStatePersisted).toBe(false)
36 expect(await inspectStagedRuntimeFirmware({ directory: item.output, target, platform, trustedRootSha256: item.fixture.rootSha256, minimumReleaseSequence: 9 })).toEqual(result.candidate)
37 expect(await inspectRuntimeTrustStore(item.reference)).toEqual(result.store)
38})
39
40test('a rotated protected root and cached metadata produce a candidate without depending on the initial root chain', async () => {
41 const item = await setup({ rotate: true })
42 const current = await refreshRuntimeTrustStore({ ...item.reference, repository: item.source, clock })
43 const protectedRoot = current.metadata.find(row => row.role === 'root')!
44 expect(protectedRoot.version).toBe(2)
45 const result = await prepareRuntimeFirmwareUpdate({ ...item.reference, repository: item.source, output: item.output, clock })
46 expect(result.candidate.trustedRootSha256).toBe(protectedRoot.sha256)
47 expect(result.candidate.rootVersion).toBe(2)
48 expect(result.store.releaseFloor).toBe(9)
49 await rename(item.source, join(item.root, 'offline'))
50 expect(await inspectStagedRuntimeFirmware({ directory: item.output, target, platform, trustedRootSha256: protectedRoot.sha256, minimumReleaseSequence: 9 })).toEqual(result.candidate)
51 await expect(inspectStagedRuntimeFirmware({ directory: item.output, target, platform, trustedRootSha256: item.fixture.rootSha256, minimumReleaseSequence: 9 })).rejects.toThrow('independent pin')
52})
53
54test('an actual output collision after metadata acceptance preserves the release floor and the competing files', async () => {
55 const item = await setup()
56 const failure = await prepareRuntimeFirmwareUpdate({ ...item.reference, repository: item.source, output: item.output, clock, async onProgress(event) {
57 if (event.phase === 'metadata-committed' && event.ordinal === 2) {
58 await mkdir(item.output)
59 await writeFile(join(item.output, 'contender.txt'), 'Preserve the competing output\n')
60 }
61 } }).then(() => undefined, error => error)
62 expect(failure.code).toBe('EEXIST')
63 expect(await readFile(join(item.output, 'contender.txt'), 'utf8')).toBe('Preserve the competing output\n')
64 await expect(stat(join(item.output, 'stage.json'))).rejects.toHaveProperty('code', 'ENOENT')
65 const stopped = await inspectRuntimeTrustStore(item.reference)
66 expect(stopped.generation).toBe(3)
67 expect(stopped.releaseFloor).toBe(9)
68 expect(stopped.attempt?.phase).toBe('maintenance')
69 const recovered = await recoverRuntimeTrustStore(item.reference)
70 expect(recovered.releaseFloor).toBe(9)
71 expect(recovered.attempt?.phase).toBe('recovered')
72 expect(recovered.physicalDeploymentAllowed).toBe(false)
73})
74
75test('candidate paths cannot overlap the repository, enrollment or protocol store', async () => {
76 const item = await setup(), initial = await inspectRuntimeTrustStore(item.reference)
77 for (const source of [item.source, item.reference.enrollmentDirectory, item.reference.directory]) {
78 await expect(prepareRuntimeFirmwareUpdate({ ...item.reference, repository: item.source, output: join(source, 'candidate'), clock })).rejects.toThrow('separate')
79 expect(await inspectRuntimeTrustStore(item.reference)).toEqual(initial)
80 }
81})
82
83test('newer signed metadata cannot prepare a release below the protected floor or overwrite an earlier candidate', async () => {
84 const root = await mkdtemp(join(tmpdir(), 'drone-prepare-floor-')); roots.push(root)
85 const source = join(root, 'repo'), fixture = await epochRepository(source)
86 const enrollmentDirectory = join(root, 'enrollment')
87 const enrolled = enrollRuntimeTrust({ target, platform, trustedRootSha256: fixture.rootSha256, minimumReleaseSequence: 7, trustedRoot: fixture.rootPath, output: enrollmentDirectory })
88 const store = await createRuntimeTrustStore({ enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, output: join(root, 'store') })
89 const reference = { enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, directory: join(root, 'store'), storeSha256: store.storeSha256 }
90 const original = await fixture.publish(1, 1, 9), output = join(root, 'candidate')
91 await prepareRuntimeFirmwareUpdate({ ...reference, repository: source, output, clock })
92 const receipt = await readFile(join(output, 'stage.json'))
93 await fixture.publish(1, 2, 8)
94 await expect(prepareRuntimeFirmwareUpdate({ ...reference, repository: source, output: join(root, 'downgrade'), clock })).rejects.toThrow('below the trusted minimum')
95 const stopped = await inspectRuntimeTrustStore(reference)
96 expect(stopped.releaseFloor).toBe(9)
97 expect(stopped.metadata.find(row => row.role === 'targets')?.version).toBe(2)
98 expect(stopped.attempt?.phase).toBe('maintenance')
99 expect(await readFile(join(output, 'image.bin'))).toEqual(original.image)
100 expect(await readFile(join(output, 'stage.json'))).toEqual(receipt)
101 const recovered = await recoverRuntimeTrustStore(reference)
102 expect(recovered.releaseFloor).toBe(9)
103 expect(recovered.attempt?.phase).toBe('recovered')
104 expect(recovered.physicalDeploymentAllowed).toBe(false)
105})
106
107test('bad image bytes cannot discard accepted metadata or release floors, including after offline recovery', async () => {
108 const item = await setup()
109 await writeFile(item.fixture.imagePath, Buffer.alloc(item.fixture.image.length, 0xff))
110 await expect(prepareRuntimeFirmwareUpdate({ ...item.reference, repository: item.source, output: item.output, clock })).rejects.toThrow()
111 const stopped = await inspectRuntimeTrustStore(item.reference)
112 expect(stopped.generation).toBe(3)
113 expect(stopped.releaseFloor).toBe(9)
114 expect(stopped.attempt?.phase).toBe('maintenance')
115 await expect(stat(item.output)).rejects.toHaveProperty('code', 'ENOENT')
116 await rename(item.source, join(item.root, 'offline'))
117 const recovered = await recoverRuntimeTrustStore(item.reference)
118 expect(recovered.generation).toBe(3)
119 expect(recovered.releaseFloor).toBe(9)
120 expect(recovered.attempt?.phase).toBe('recovered')
121 expect(recovered.physicalDeploymentAllowed).toBe(false)
122 await rename(join(item.root, 'offline'), item.source)
123 await writeFile(item.fixture.imagePath, item.fixture.image)
124 const fresh = await prepareRuntimeFirmwareUpdate({ ...item.reference, repository: item.source, output: item.output, clock })
125 expect(fresh.store.releaseFloor).toBe(9)
126 expect(fresh.candidate.sha256).toBe(item.fixture.imageHash)
127 expect(fresh.physicalDeploymentAllowed).toBe(false)
128})
129
130test('real preparation process cuts retain accepted floors and never turn recovery into candidate authorization', async () => {
131 const root = await mkdtemp(join(tmpdir(), 'drone-prepare-host-cuts-')); roots.push(root)
132 const result = await qualifyPreparationInterruptions(join(root, 'campaign'))
133 expect(result.cuts).toBe(2)
134 expect(result.physicalDeploymentAllowed).toBe(false)
135}, 45000)
136
137test('protected preparation also archives repositories without consistent-snapshot filename prefixes', async () => {
138 const item = await setup({ rotate: true, consistentSnapshot: false })
139 const result = await prepareRuntimeFirmwareUpdate({ ...item.reference, repository: item.source, output: item.output, clock })
140 const current = await inspectRuntimeTrustStore(item.reference), root = current.metadata.find(row => row.role === 'root')!
141 expect(root.version).toBe(2)
142 expect(result.candidate.sha256).toBe(item.fixture.imageHash)
143 expect(await inspectStagedRuntimeFirmware({ directory: item.output, target, platform, trustedRootSha256: root.sha256, minimumReleaseSequence: current.releaseFloor })).toEqual(result.candidate)
144 expect(result.physicalDeploymentAllowed).toBe(false)
145})
146
147test('actual candidate-image corruption after file sync is rejected by readback before a completion marker', async () => {
148 const item = await setup(), damaged = Buffer.alloc(item.fixture.image.length, 0xff)
149 let changedAfterWrite = false
150 await expect(prepareRuntimeFirmwareUpdate({ ...item.reference, repository: item.source, output: item.output, clock, async onProgress(event) {
151 if (event.phase === 'image-written') {
152 await writeFile(join(item.output, 'image.bin'), damaged)
153 changedAfterWrite = true
154 }
155 } })).rejects.toThrow()
156 expect(changedAfterWrite).toBe(true)
157 expect(await readFile(join(item.output, 'image.bin'))).toEqual(damaged)
158 expect(await Bun.file(join(item.output, 'stage.json')).exists()).toBe(false)
159 const stopped = await inspectRuntimeTrustStore(item.reference)
160 expect(stopped.releaseFloor).toBe(9)
161 expect(stopped.attempt?.phase).toBe('maintenance')
162 expect(stopped.physicalDeploymentAllowed).toBe(false)
163})