Files
MikrotikManager/backend/src/services/server-poll-journal-snapshot.test.ts
T

188 lines
18 KiB
TypeScript

/** Component control flow with actual journal/private bridge/storage factories.
* SQL is an explicit in-memory double; AES/HMAC are real but use fixture keys.
* This does not claim PostgreSQL lock or production policy acceptance. */
import assert from "node:assert/strict"
import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes, randomUUID } from "node:crypto"
import { readFileSync } from "node:fs"
import { test } from "node:test"
import { runInNewContext } from "node:vm"
import { isDeepStrictEqual } from "node:util"
import { publicationLayout, serverPollMaintenancePublications } from "./server-poll-maintenance-groups.js"
import ts from "typescript"
import { z } from "zod"
import * as errorProvenance from "./server-poll-error-provenance.js"
import { createServerPollStorageForTests, PollStorageUnknown, PollStorageRefused } from "./server-poll-storage.js"
import { SERVER_POLL_NATIVE_READS } from "./server-poll-execution-model.js"
import { encodeServerPollJournalAnchor, openServerPollJournalAnchor } from "./server-poll-journal-anchor.js"
import { validateServerPollJournalReceipt } from "./server-poll-journal-validation.js"
import type { ServerPollJournalContext, ServerPollJournal } from "./server-poll-journal.js"
import type { ServerPollJournalStorage, ServerPollStorageJournalOptions } from "./server-poll-storage-journal.js"
import type { ProbeJournalCrypto } from "./server-probe-journal.js"
type Row = Record<string, unknown>
type State = { admission?: Row; anchors: Row[]; intents: Row[]; acks: Row[]; receipts: Row[]; blocked: Row[] }
const canonical = (value: unknown): string => JSON.stringify(value, (_key, item: unknown) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item)
const fingerprint = (value: unknown) => createHash("sha256").update(canonical(value)).digest("hex")
const compile = (name: string) => ts.transpileModule(readFileSync(new URL(name, import.meta.url), "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText
const bridgeCode = compile("./server-poll-storage-journal.ts"), journalCode = compile("./server-poll-journal.ts")
function load(code: string, modules: Record<string, unknown>) {
const exports: Record<string, unknown> = {}
runInNewContext(code, { exports, require(name: string) { assert(Object.hasOwn(modules, name), name); return modules[name] },
Date, Error, Object, Promise, Number, String, Buffer, JSON, RegExp, Map, Set, WeakMap, structuredClone, setTimeout, clearTimeout }, { filename: "actual-poll-snapshot-component.js" })
return exports
}
async function fixture() {
const now = Date.now(), key = randomBytes(32), id = randomUUID(), hash = "a".repeat(64), controller = new AbortController()
const proof = async (value: unknown, domain: string) => createHmac("sha256", key).update(domain).update(fingerprint(value)).digest("hex")
const raw: ServerPollJournalContext = { identity: { kind: "rest", ownerId: "owned-component", executionId: randomUUID(), requestId: randomUUID(), authentication: "local", issuedAt: now - 1000, expiresAt: now + 60000 },
plan: { version: 1, requiresHuman: true, scope: "global", request: { serverId: 1, receiptId: id }, around: new Date(now).toISOString(), beforeProof: hash, authorityProof: hash, bindingProof: hash,
affectedServerIds: [1], maintenance: [{ kind: "partition.drop", parent: "server_snapshots", name: "server_snapshots_d_20200101" }], mutationCost: 9 } }
const { bindingProof: _binding, ...core } = raw.plan; raw.plan.bindingProof = await proof(core, "production")
let state: State = { anchors: [], intents: [], acks: [], receipts: [], blocked: [] }, authority = true
let decryptHook: (() => void) | undefined, snapshotHook: ((value: Row) => void) | undefined, authorityBeforeSnapshot = false, authorityAfterSnapshot = false
const calls: string[] = [], releases: boolean[] = [], verifiedMacs: { domain: string; stage?: string }[] = [], events: string[] = []
const authorityCheck = async () => { events.push("authority"); if (!authority || controller.signal.aborted) throw new PollStorageRefused() }
const crypto: ProbeJournalCrypto = {
async encrypt(text) { const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv); const data = Buffer.concat([cipher.update(text), cipher.final()]); return Buffer.concat([iv, cipher.getAuthTag(), data]).toString("base64") },
async decrypt(text) { const data = Buffer.from(text, "base64"), cipher = createDecipheriv("aes-256-gcm", key, data.subarray(0, 12)); cipher.setAuthTag(data.subarray(12, 28)); const plain = Buffer.concat([cipher.update(data.subarray(28)), cipher.final()]).toString(); decryptHook?.(); return plain },
async proof(domain, value) { verifiedMacs.push({ domain, stage: value && typeof value === "object" && "stage" in value ? String(value.stage) : undefined }); return proof(value, "server-poll-journal-v1:" + domain) },
}
const connect: Parameters<typeof createServerPollStorageForTests>[1]["connect"] = async () => {
let before: State | undefined
return { release(unsafe = false) { releases.push(unsafe) }, async query(text, input = []) {
const values = Array.from(input); calls.push(text); events.push(text)
if (text === "BEGIN") { before = structuredClone(state); return { rows: [] } }
if (text === "COMMIT") { before = undefined; return { rows: [] } }
if (text === "ROLLBACK") { assert(before); state = before; before = undefined; return { rows: [] } }
if (text.startsWith("SELECT set_config(") || text.includes("pg_advisory_xact_lock") || text.startsWith("SET LOCAL")) return { rows: [] }
if (text.startsWith("SELECT a.receipt_id AS admission_receipt_id") || text.startsWith("SELECT r.receipt_id FROM server_poll_archives")) return { rows: [] }
if (text.startsWith("SELECT scope,admissions,compacted_admissions")) return { rows: [{ scope: "global", admissions: 0, compacted_admissions: 0, archive_bytes: 0, reserved_rows: 0, reserved_bytes: 0 }] }
if (text.startsWith("SELECT * FROM server_poll_storage_admissions")) return { rows: state.admission ? [structuredClone(state.admission)] : [] }
if (text.startsWith("INSERT INTO server_poll_storage_admissions")) { const names = text.slice(text.indexOf("(") + 1, text.indexOf(")")).split(","); state.admission = Object.fromEntries(names.slice(0, values.length).map((name, index) => [name, values[index]])); state.admission.storage_state = "full"; state.admission.claim_attempted_at = null; return { rows: [{ receipt_id: id }] } }
if (text.startsWith("UPDATE server_poll_storage_admissions SET claim_token=NULL")) { assert(state.admission); state.admission.claim_token = null; state.admission.claim_attempted_at = new Date(now); return { rows: [{ receipt_id: id }] } }
if (text.includes("poll-storage-journal:existing-anchor")) return { rows: state.anchors.map(row => ({ receipt_id: row.receipt_id, identity_kind: row.identity_kind, execution_id: row.execution_id })) }
const tag = text.match(/poll-journal:([\w-]+)/)?.[1]
const stages = () => state.intents.map(row => ({ ...row, result_proof: null, outcome: null, ack_proof: null, ...state.acks.find(ack => ack.stage === row.stage) }))
if (tag === "servers") return { rows: [{ id: 1, enabled: true }] }
if (tag === "blocked") return { rows: structuredClone(state.blocked) }
if (tag === "anchor") return { rows: structuredClone(state.anchors) }
if (tag === "terminal") return { rows: structuredClone(state.receipts) }
if (tag === "stages") return { rows: structuredClone(stages()) }
if (tag === "write-snapshot") {
authorityBeforeSnapshot = events.at(-2) === "authority"
const value: Row = structuredClone({ blocked: state.blocked, anchors: state.anchors, terminal: state.receipts, stages: stages() })
snapshotHook?.(value); return { rows: [{ snapshot: value }] }
}
const row = (names: string[]) => Object.fromEntries(names.map((name, index) => [name, values[index]]))
if (tag === "claim") { const value = row(["receipt_id", "identity_kind", "execution_id", "binding_proof", "context_ciphertext", "context_proof"]); state.anchors.push(value); return { rows: [value] } }
if (tag === "intent") { authorityAfterSnapshot = events.lastIndexOf("authority") > events.reduce((last, event, index) => event.includes("write-snapshot") ? index : last, -1); const value = row(["receipt_id", "stage", "before_proof", "effect_proof", "intent_proof"]); state.intents.push(value); return { rows: [value] } }
if (tag === "ack") { const value = row(["receipt_id", "stage", "result_proof", "outcome", "ack_proof"]); state.acks.push(value); return { rows: [value] } }
if (tag === "complete") { const value = row(["receipt_id", "receipt_ciphertext", "receipt_proof"]); state.receipts.push(value); return { rows: [value] } }
throw Error("Unexpected component SQL: " + text)
} }
}
// The private WeakMap entry is issued ONLY by the actual production bridge
// factory. Its actual storage factory owns transaction fences over SQL double.
const groupMethods = {
publicationLayout: (plan: Parameters<typeof publicationLayout>[0]) => publicationLayout(structuredClone(plan)),
serverPollMaintenancePublications: (plan: Parameters<typeof serverPollMaintenancePublications>[0]) => serverPollMaintenancePublications(structuredClone(plan)),
}
const bridge = load(bridgeCode, { "../modules/mcp/security.js": { fingerprint, privateSecretFingerprint: proof },
"./server-poll-maintenance-groups.js": groupMethods,
"./server-partition-maintenance-lease.js": { isServerPollJournalOwner: () => assert.fail("Snapshot fixture uses standalone journal storage") },
"./server-poll-storage.js": { createServerPollStorage: (binding: Parameters<typeof createServerPollStorageForTests>[0]) => createServerPollStorageForTests(binding, { now: Date.now, connect }), PollStorageRefused } }) as unknown as {
createServerPollStorageJournal(context: ServerPollJournalContext, options: ServerPollStorageJournalOptions): Promise<ServerPollJournalStorage>
bindServerPollJournalAuthority(storage: ServerPollJournalStorage, context: ServerPollJournalContext, provider: ServerPollStorageJournalOptions["assertAuthority"]): unknown
PollStorageSpent: typeof PollStorageRefused
}
const storage = await bridge.createServerPollStorageJournal(raw, { deadlineAt: now + 60000, signal: controller.signal, assertAuthority: authorityCheck })
assert.equal(typeof bridge.bindServerPollJournalAuthority(storage, raw, authorityCheck), "function")
assert.equal(bridge.bindServerPollJournalAuthority({ ...storage }, raw, authorityCheck), undefined, "Copying the facade must not forge private ownership")
// Normalize VM array/object prototypes before the real strict value comparer;
// production receives one realm, while this harness crosses a VM boundary.
const journalModule = load(journalCode, { "./server-poll-error-provenance.js": errorProvenance, zod: { z }, "node:util": { isDeepStrictEqual: (left: unknown, right: unknown) => isDeepStrictEqual(structuredClone(left), structuredClone(right)) }, "./server-poll-limits.js": { SERVER_POLL_WALL_BUDGET_MS: 180000 },
"./server-poll-maintenance-groups.js": groupMethods,
"./server-poll-storage.js": { PollStorageUnknown }, "./server-poll-storage-journal.js": bridge,
"./server-poll-journal-anchor.js": {
encodeServerPollJournalAnchor: (context: Parameters<typeof encodeServerPollJournalAnchor>[0], start: Parameters<typeof encodeServerPollJournalAnchor>[1], signer: Parameters<typeof encodeServerPollJournalAnchor>[2]) => encodeServerPollJournalAnchor(structuredClone(context), structuredClone(start), signer),
openServerPollJournalAnchor: (value: unknown, context: Parameters<typeof openServerPollJournalAnchor>[1], signer: Parameters<typeof openServerPollJournalAnchor>[2]) => openServerPollJournalAnchor(structuredClone(value), structuredClone(context), signer),
},
"./server-poll-execution-model.js": { SERVER_POLL_NATIVE_READS }, "./server-poll-journal-validation.js": { validateServerPollJournalReceipt } }) as unknown as { createSqlServerPollJournal: (context: ServerPollJournalContext, deps: Record<string, unknown>) => ServerPollJournal; ServerPollJournalConflict: new () => Error }
const journal = journalModule.createSqlServerPollJournal(raw, { storage, crypto, now: Date.now, executionProof: (value: unknown) => proof(value, "production"), assertAuthority: authorityCheck })
await journal.claim(raw.plan.request, raw.plan.bindingProof)
const next = () => journal.intent({ receiptId: id, stage: "publish.0", beforeProof: hash, effectProof: hash })
return { raw, journal, next, calls, releases, events, verifiedMacs, state: () => state, Conflict: journalModule.ServerPollJournalConflict,
revoke: () => { authority = false }, abort: () => controller.abort(), duringDecrypt: (hook: () => void) => { decryptHook = hook }, snapshot: (hook: (value: Row) => void) => { snapshotHook = hook },
flags: () => ({ authorityBeforeSnapshot, authorityAfterSnapshot }), proof, hash }
}
async function natives(f: Awaited<ReturnType<typeof fixture>>) {
for (const read of SERVER_POLL_NATIVE_READS) { const value = { receiptId: f.raw.plan.request.receiptId, stage: "native." + read, beforeProof: f.hash, effectProof: f.hash }; await f.journal.intent(value); await f.journal.acknowledge({ receiptId: value.receiptId, stage: value.stage, resultProof: f.hash, outcome: "confirmed" }) }
}
test("actual private factory snapshot validates every retained row MAC and fences actual DML", async () => {
const f = await fixture(); await natives(f); f.calls.length = 0; f.verifiedMacs.length = 0
await f.next()
assert.equal(f.calls.filter(sql => sql.includes("write-snapshot")).length, 1)
assert(!f.calls.some(sql => /poll-journal:(?:blocked|anchor|terminal|stages)\s/.test(sql)))
for (const read of SERVER_POLL_NATIVE_READS) for (const domain of ["intent", "ack"]) assert(f.verifiedMacs.some(value => value.domain === domain && value.stage === "native." + read))
assert.deepEqual(f.flags(), { authorityBeforeSnapshot: true, authorityAfterSnapshot: true })
const snapshot = f.calls.find(sql => sql.includes("write-snapshot"))!
assert(snapshot.includes("NOT EXISTS(SELECT 1 FROM blocked) FOR UPDATE OF e")); assert(snapshot.includes("ORDER BY i.stage LIMIT 2009"))
})
test("each old retained intent and ACK corruption refuses a later append", async () => {
for (const index of [0, 1, 2, 3]) for (const kind of ["intent", "ack"] as const) {
const f = await fixture(); await natives(f)
if (kind === "intent") f.state().intents[index].intent_proof = "b".repeat(64); else f.state().acks[index].ack_proof = "b".repeat(64)
const before = f.state().intents.length; await assert.rejects(f.next(), f.Conflict); assert.equal(f.state().intents.length, before)
}
})
test("snapshot preconditions and malformed complete prefix never write", async () => {
for (const mutate of [
(value: Row) => { value.blocked = [{}] }, (value: Row) => { value.anchors = [] },
(value: Row) => { value.anchors = [{ ...(value.anchors as Row[])[0], execution_id: randomUUID() }] },
(value: Row) => { value.anchors = [...value.anchors as Row[], ...(value.anchors as Row[])] },
(value: Row) => { value.terminal = [{}] }, (value: Row) => { value.stages = null },
(value: Row) => { value.stages = Array(10).fill({}) },
(value: Row) => { (value.anchors as Row[])[0].context_ciphertext = "invalid" },
(value: Row) => { (value.anchors as Row[])[0].context_proof = "b".repeat(64) },
]) { const f = await fixture(); await natives(f); f.snapshot(mutate); await assert.rejects(f.next(), f.Conflict); assert.equal(f.state().intents.length, 4) }
})
test("abort or revocation during actual anchor decrypt refuses before INSERT", async () => {
for (const mode of ["abort", "revoke"] as const) {
const f = await fixture(); await natives(f); f.calls.length = 0; f.duringDecrypt(() => mode === "abort" ? f.abort() : f.revoke())
await assert.rejects(f.next(), f.Conflict)
assert(!f.calls.some(sql => sql.includes("poll-journal:intent"))); assert(!f.calls.includes("COMMIT"))
if (mode === "abort") assert.equal(f.releases.at(-1), true); else assert(f.calls.includes("ROLLBACK"))
}
})
test("sequence, duplicate and missing native ACK semantics remain enforced", async () => {
const f = await fixture(); await assert.rejects(f.next(), f.Conflict); await natives(f)
await assert.rejects(f.journal.intent({ receiptId: f.raw.plan.request.receiptId, stage: "publish.0", beforeProof: "b".repeat(64), effectProof: f.hash }), f.Conflict)
await assert.rejects(f.journal.intent({ receiptId: f.raw.plan.request.receiptId, stage: "publish.1", beforeProof: f.hash, effectProof: f.hash }), f.Conflict)
await f.next(); await assert.rejects(f.next(), f.Conflict)
const ack = { receiptId: f.raw.plan.request.receiptId, stage: "publish.0", resultProof: f.hash, outcome: "confirmed" as const }
await f.journal.acknowledge(ack); await assert.rejects(f.journal.acknowledge(ack), f.Conflict)
const missing = await fixture(); await natives(missing); missing.state().acks.pop(); await assert.rejects(missing.next(), missing.Conflict)
})
test("revocation before or immediately after the snapshot never reaches INSERT", async () => {
for (const mode of ["before", "after"] as const) {
const f = await fixture(); await natives(f); f.calls.length = 0
if (mode === "before") f.revoke(); else f.snapshot(() => f.revoke())
await assert.rejects(f.next(), f.Conflict)
assert(!f.calls.some(sql => sql.includes("poll-journal:intent")))
if (mode === "before") assert(!f.calls.some(sql => sql.includes("write-snapshot")))
}
})
test("authentic retained MACs cannot disguise a wrong maintenance effect", async () => {
const f = await fixture(); await natives(f)
for (let index = 0; index < 3; index++) {
const stage = "publish." + index
await f.journal.intent({ receiptId: f.raw.plan.request.receiptId, stage, beforeProof: f.hash, effectProof: f.hash })
await f.journal.acknowledge({ receiptId: f.raw.plan.request.receiptId, stage, resultProof: f.hash, outcome: "confirmed" })
}
await assert.rejects(f.journal.complete({ receiptId: f.raw.plan.request.receiptId, configurationVerified: true, complete: true, maintenanceComplete: true,
nativeReads: 4, confirmedPublications: 3, completedPublications: 3, afterProof: f.hash, observation: "online",
snapshot: { id: 1, serverId: 1, status: "online", polledAt: f.raw.plan.around } }, f.raw.plan.bindingProof), f.Conflict)
assert.equal(f.state().receipts.length, 0)
})