also looking at this
feat: own runtime firmware candidates
#49
24 files
+1245
-92
| @@ -3,12 +3,13 @@ import { spawnSync } from 'node:child_process' | ||
| 3 | 3 | import { mkdir, readFile, writeFile } from 'node:fs/promises' |
| 4 | 4 | import { join, resolve } from 'node:path' |
| 5 | 5 | import { enrollRuntimeTrust } from '../../../resources/functions/drone/updates/enrollment' |
| 6 | import { createRuntimeTrustStore, inspectRuntimeTrustStore, recoverRuntimeTrustStore, refreshRuntimeTrustStore } from '../../../resources/functions/drone/updates/store' | |
| 6 | import { createRuntimeTrustStore, inspectRuntimeFirmwareCandidates, inspectRuntimeTrustStore, prepareRuntimeFirmwareUpdate, recoverRuntimeTrustStore, refreshRuntimeTrustStore, reserveRuntimeFirmwareCandidate, retireRuntimeFirmwareCandidate } from '../../../resources/functions/drone/updates/store' | |
| 7 | import type { RuntimeUpdateProgress } from '../../../resources/functions/drone/updates/store' | |
| 7 | 8 | import { platform, repository, target } from './repository' |
| 8 | 9 | |
| 9 | 10 | type Reference = Parameters<typeof inspectRuntimeTrustStore>[0] |
| 10 | type Cut = 'input-committed' | 'metadata-committed' | |
| 11 | interface WorkerInput { reference: Reference, repository: string, cut: Cut } | |
| 11 | type Cut = RuntimeUpdateProgress['phase'] | |
| 12 | interface WorkerInput { reference: Reference, repository: string, cut: Cut, output?: string, managed?: boolean, candidateId?: string } | |
| 12 | 13 | const clock = { source: 'synthetic-independent-clock', read: () => new Date('2030-01-01T00:00:00Z') } |
| 13 | 14 | |
| 14 | 15 | async function worker(mode: string, file: string) { |
| @@ -23,27 +24,35 @@ async function worker(mode: string, file: string) { | ||
| 23 | 24 | return |
| 24 | 25 | } |
| 25 | 26 | if (mode !== 'hold') throw new Error('Unknown interruption worker mode') |
| 26 | await refreshRuntimeTrustStore({ ...input.reference, repository: input.repository, clock, async onProgress(event) { | |
| 27 | const options = { ...input.reference, repository: input.repository, clock, async onProgress(event: Readonly<RuntimeUpdateProgress>) { | |
| 27 | 28 | if (event.phase !== input.cut || event.ordinal !== (input.cut === 'input-committed' ? 1 : 0)) return |
| 28 | 29 | console.log(JSON.stringify({ ready: true, phase: event.phase, ordinal: event.ordinal })) |
| 29 | 30 | await Bun.stdin.text() |
| 30 | } }) | |
| 31 | } } | |
| 32 | if (input.candidateId) await retireRuntimeFirmwareCandidate({ ...options, candidateId: input.candidateId }) | |
| 33 | else if (input.managed) await reserveRuntimeFirmwareCandidate(options) | |
| 34 | else if (input.output) await prepareRuntimeFirmwareUpdate({ ...options, output: input.output }) | |
| 35 | else await refreshRuntimeTrustStore(options) | |
| 31 | 36 | throw new Error('Interruption worker unexpectedly finished') |
| 32 | 37 | } |
| 33 | 38 | |
| 34 | 39 | /** Keep generated signed repositories and actual host process outcomes for inspection. No power-loss claim. */ |
| 35 | export async function qualifyStoreInterruptions(directory: string) { | |
| 40 | async function qualifyInterruptions(directory: string, operation: 'refresh' | 'prepare' | 'reserve') { | |
| 36 | 41 | await mkdir(directory, { mode: 0o700 }) |
| 37 | 42 | const results = [] |
| 38 | for (const cut of ['input-committed', 'metadata-committed'] as const) { | |
| 43 | const managed = operation === 'reserve', preparation = operation !== 'refresh' | |
| 44 | const cuts: Cut[] = managed ? ['candidate-reserved', 'image-written', 'candidate-completed', 'candidate-recorded', 'candidate-retired'] : preparation ? ['image-written', 'candidate-completed'] : ['input-committed', 'metadata-committed'] | |
| 45 | for (const cut of cuts) { | |
| 39 | 46 | const root = join(directory, cut); await mkdir(root, { mode: 0o700 }) |
| 40 | 47 | const source = join(root, 'repo'), fixture = await repository(source, { custom: { releaseSequence: 9 } }) |
| 41 | 48 | const enrollmentDirectory = join(root, 'enrollment') |
| 42 | 49 | const enrolled = enrollRuntimeTrust({ target, platform, trustedRootSha256: fixture.rootSha256, minimumReleaseSequence: 7, trustedRoot: fixture.rootPath, output: enrollmentDirectory }) |
| 43 | 50 | const created = await createRuntimeTrustStore({ enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, output: join(root, 'store') }) |
| 44 | 51 | const reference = { enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, directory: join(root, 'store'), storeSha256: created.storeSha256 } |
| 52 | const prior = cut === 'candidate-retired' ? await reserveRuntimeFirmwareCandidate({ ...reference, repository: source, clock }) : null | |
| 45 | 53 | const file = join(root, 'worker.json') |
| 46 | await writeFile(file, JSON.stringify({ reference, repository: source, cut } satisfies WorkerInput), { mode: 0o600 }) | |
| 54 | let output = prior?.candidate?.directory ?? (!managed && preparation ? join(root, 'candidate') : undefined) | |
| 55 | await writeFile(file, JSON.stringify({ reference, repository: source, cut, output: managed ? undefined : output, managed, candidateId: prior?.candidate?.id } satisfies WorkerInput), { mode: 0o600 }) | |
| 47 | 56 | const child = Bun.spawn([process.execPath, import.meta.path, 'hold', file], { stdin: 'pipe', stdout: 'pipe', stderr: 'pipe' }) |
| 48 | 57 | const timer = setTimeout(() => child.kill('SIGKILL'), 15000) |
| 49 | 58 | const reader = child.stdout.getReader(), decoder = new TextDecoder() |
| @@ -64,15 +73,42 @@ export async function qualifyStoreInterruptions(directory: string) { | ||
| 64 | 73 | const exit = await child.exited, signal = child.signalCode |
| 65 | 74 | assert.equal(signal, 'SIGKILL'); assert.notEqual(exit, 0) |
| 66 | 75 | const interrupted = await inspectRuntimeTrustStore(reference) |
| 67 | assert.equal(interrupted.attempt?.phase, 'pending') | |
| 68 | assert.equal(interrupted.generation, cut === 'input-committed' ? 0 : 1) | |
| 69 | const recovered = await recoverRuntimeTrustStore(reference) | |
| 70 | assert.equal(recovered.generation, 1); assert.equal(recovered.attempt?.phase, 'recovered') | |
| 71 | assert.equal(recovered.releaseFloor, 7) | |
| 72 | const fresh = await refreshRuntimeTrustStore({ ...reference, repository: source, clock }) | |
| 73 | assert.equal(fresh.generation, 3); assert.equal(fresh.releaseFloor, 9) | |
| 76 | assert.equal(interrupted.attempt?.phase, cut === 'candidate-retired' ? 'verified' : 'pending') | |
| 77 | assert.equal(interrupted.generation, preparation ? 3 : cut === 'input-committed' ? 0 : 1) | |
| 78 | assert.equal(interrupted.releaseFloor, preparation ? 9 : 7) | |
| 79 | const candidateBefore = managed ? await inspectRuntimeFirmwareCandidates(reference) : null | |
| 80 | if (managed && cut !== 'candidate-retired') { | |
| 81 | assert.equal(candidateBefore!.candidate!.phase, cut === 'candidate-recorded' ? 'prepared' : 'reserved') | |
| 82 | assert.equal(candidateBefore!.candidate!.currentTrustValidated, false) | |
| 83 | output = candidateBefore!.candidate!.directory | |
| 84 | } | |
| 85 | if (cut === 'candidate-retired') assert.equal(candidateBefore!.candidate, null) | |
| 86 | const candidateStagePresent = output ? await Bun.file(join(output, 'stage.json')).exists() : false | |
| 87 | if (output) { | |
| 88 | if (cut !== 'candidate-reserved') assert.deepEqual(await readFile(join(output, 'image.bin')), fixture.image) | |
| 89 | assert.equal(candidateStagePresent, ['candidate-completed', 'candidate-recorded', 'candidate-retired'].includes(cut)) | |
| 90 | } | |
| 91 | const recovered = cut === 'candidate-retired' ? await inspectRuntimeTrustStore(reference) : await recoverRuntimeTrustStore(reference) | |
| 92 | assert.equal(recovered.generation, preparation ? 3 : 1); assert.equal(recovered.attempt?.phase, cut === 'candidate-retired' ? 'verified' : 'recovered') | |
| 93 | assert.equal(recovered.releaseFloor, preparation ? 9 : 7) | |
| 94 | assert.equal(Object.hasOwn(recovered, 'candidate'), false) | |
| 95 | if (managed) { | |
| 96 | assert.deepEqual((await inspectRuntimeFirmwareCandidates(reference)).candidate, candidateBefore!.candidate) | |
| 97 | if (candidateBefore!.candidate) await retireRuntimeFirmwareCandidate({ ...reference, candidateId: candidateBefore!.candidate.id }) | |
| 98 | } | |
| 99 | const completed = managed ? await reserveRuntimeFirmwareCandidate({ ...reference, repository: source, clock }) : preparation ? await prepareRuntimeFirmwareUpdate({ ...reference, repository: source, clock, output: join(root, 'fresh-candidate') }) : await refreshRuntimeTrustStore({ ...reference, repository: source, clock }) | |
| 100 | const fresh = completed.schema === 'drone-runtime-prepared-image-v1' || completed.schema === 'drone-runtime-candidates-v1' ? completed.store : completed | |
| 101 | if (completed.schema === 'drone-runtime-candidates-v1') { | |
| 102 | assert.equal(completed.retainedCandidates, 2) | |
| 103 | assert.equal(completed.candidate!.integrity, 'recorded-bytes-match') | |
| 104 | assert.notEqual(completed.candidate!.id, candidateBefore!.candidate?.id ?? prior!.candidate!.id) | |
| 105 | if (candidateStagePresent) assert.deepEqual(await readFile(join(output!, 'image.bin')), fixture.image) | |
| 106 | } | |
| 107 | assert.equal(fresh.generation >= interrupted.generation, true) | |
| 108 | if (!preparation) assert.equal(fresh.generation, 3) | |
| 109 | assert.equal(fresh.releaseFloor, 9) | |
| 74 | 110 | assert.equal(fresh.attempt?.phase, 'verified'); assert.equal(fresh.physicalDeploymentAllowed, false) |
| 75 | results.push({ cut, ready, killedExit: exit, killedSignal: signal, contenderExit: contender.status, contender: contender.stdout, interrupted, recovered, fresh }) | |
| 111 | results.push({ cut, ready, killedExit: exit, killedSignal: signal, contenderExit: contender.status, contender: contender.stdout, candidateStagePresent, candidateBefore, interrupted, recovered, fresh }) | |
| 76 | 112 | } |
| 77 | 113 | finally { |
| 78 | 114 | clearTimeout(timer) |
| @@ -81,14 +117,20 @@ export async function qualifyStoreInterruptions(directory: string) { | ||
| 81 | 117 | reader.releaseLock() |
| 82 | 118 | await writeFile(join(root, 'worker.stdout'), stdout) |
| 83 | 119 | await writeFile(join(root, 'worker.stderr'), await new Response(child.stderr).text()) |
| 84 | await writeFile(join(directory, 'qualification.json'), JSON.stringify({ evidence: 'host-process-interruption-only', results, physicalDeploymentAllowed: false }, null, 2)) | |
| 120 | await writeFile(join(directory, 'qualification.json'), JSON.stringify({ evidence: 'host-process-interruption-only', operation, results, physicalDeploymentAllowed: false }, null, 2)) | |
| 85 | 121 | } |
| 86 | 122 | } |
| 87 | return { evidence: 'host-process-interruption-only', cuts: results.length, physicalDeploymentAllowed: false } | |
| 123 | return { evidence: 'host-process-interruption-only', operation, cuts: results.length, physicalDeploymentAllowed: false } | |
| 88 | 124 | } |
| 89 | 125 | |
| 126 | export async function qualifyStoreInterruptions(directory: string) { return qualifyInterruptions(directory, 'refresh') } | |
| 127 | export async function qualifyPreparationInterruptions(directory: string) { return qualifyInterruptions(directory, 'prepare') } | |
| 128 | export async function qualifyCandidateInterruptions(directory: string) { return qualifyInterruptions(directory, 'reserve') } | |
| 129 | ||
| 90 | 130 | if (import.meta.main) { |
| 91 | if (process.argv.length === 4) await worker(process.argv[2]!, process.argv[3]!) | |
| 131 | if (process.argv.length === 4 && process.argv[2] === 'reserve') console.log(JSON.stringify(await qualifyCandidateInterruptions(resolve(process.argv[3]!)))) | |
| 132 | else if (process.argv.length === 4 && process.argv[2] === 'prepare') console.log(JSON.stringify(await qualifyPreparationInterruptions(resolve(process.argv[3]!)))) | |
| 133 | else if (process.argv.length === 4) await worker(process.argv[2]!, process.argv[3]!) | |
| 92 | 134 | else if (process.argv.length === 3) console.log(JSON.stringify(await qualifyStoreInterruptions(resolve(process.argv[2]!)))) |
| 93 | else throw new Error('Use store-interruptions.ts NEW_DIRECTORY') | |
| 135 | else throw new Error('Use store-interruptions.ts [prepare|reserve] NEW_DIRECTORY') | |
| 94 | 136 | } |