Files
MikrotikManager/backend/src/services/bandwidth-operator-registry.test.ts
T

194 lines
18 KiB
TypeScript

import assert from "node:assert/strict"
import { createHash, createHmac, generateKeyPairSync, randomUUID, sign } from "node:crypto"
import { after, test } from "node:test"
import { once } from "node:events"
import { createServer } from "node:http"
import { mkdtemp, rm, writeFile } from "node:fs/promises"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { createBandwidthOperatorRegistry, type BandwidthOperatorRegistryDependencies } from "./bandwidth-operator-registry.js"
import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js"
import type { BandwidthPeerRef } from "./bandwidth-peer-reference.js"
import { createBandwidthOperatorEnrollmentPrerequisite, bandwidthOperatorEnrollmentGrantSchema } from "./bandwidth-operator-enrollment.js"
import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js"
type Row = Record<string, unknown>
const scope = (ms = 4000) => ({ deadlineAt: Date.now() + ms })
const root = { installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) }
const mac = (purpose: string, value: unknown) => createHmac("sha256", "SYNTHETIC_OPERATOR_REGISTRY_ISSUER")
.update(purpose + ":" + canonicalBandwidthNodeValue(value)).digest("hex")
const server = createServer((req, res) => {
assert.equal(req.method, "GET"); assert.equal(req.url, "/rest/system/license")
assert.equal(req.headers.authorization, "Basic " + Buffer.from("SYNTHETIC-OPERATOR:SYNTHETIC-PASSWORD").toString("base64"))
res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify({ "system-id": "SYNTHETIC-SHARED-CLAIM" }))
})
server.listen(0, "127.0.0.1"); await once(server, "listening")
const address = server.address(); assert(address && typeof address !== "string")
const port = address.port, cleanup: Array<() => Promise<void>> = []
after(async () => { for (const close of cleanup) await close(); server.closeAllConnections(); server.close(); await once(server, "close"); const { closePool } = await import("../db/index.js"); await closePool() })
/** Genuine module-branded coordinator, signed synthetic operator files and
* authenticated native localhost GET; only SQL and executor fence are inert. */
async function fixture(atomic=true) {
const nodes: Row[] = [], managed: Row[] = [], external: Row[] = [], managedEvents: Row[] = [], externalEvents: Row[] = []
const queries: string[] = [], purposes: string[] = [], signedMaterials: Array<{purpose:string;value:Row;mac:string}> = []
const occupancy = Object.freeze({ state: "unknown", canonicalNode: "existing-occupied-node" })
let opens = 0, closes = 0, inspections = 0, batches = 0, revoked = false, badBinding = false, commitMode: "ok" | "lost" | "rows" | "hold" = "ok"
let hook: ((stage: string) => void | Promise<void>) | undefined, openHook: (() => Promise<void>) | undefined
const folder = await mkdtemp(join(tmpdir(), "operator-registry-")), keys = generateKeyPairSync("ed25519")
const trustKeyFile = join(folder, "operator.pub"), grantFile = join(folder, "grant.json")
await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" }))
let currentSource: BandwidthPeerSourceSnapshot | undefined, lastGrant: ReturnType<typeof bandwidthOperatorEnrollmentGrantSchema.parse> | undefined
const publish = async (value: NonNullable<typeof lastGrant>) => {
lastGrant = value; const payload = Buffer.from(canonicalBandwidthNodeValue(value))
await writeFile(grantFile, JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") }))
}
const coordinator = createBandwidthOperatorEnrollmentPrerequisite({ ...root, trustKeyFile, grantFile,
trustKeySha256: createHash("sha256").update(keys.publicKey.export({ type: "spki", format: "der" })).digest("hex") },
{ async read() { return currentSource } }, { async verify() { inspections++; await hook?.("inspect"); if (revoked) throw Error("Original synthetic executor fence revoked") } })
cleanup.push(async () => { coordinator.close(); await rm(folder, { recursive: true, force: true }) })
const source: BandwidthOperatorRegistryDependencies["source"] = { async open(_scope, dispatchGuard) {
opens++; await dispatchGuard(); await openHook?.()
const before = structuredClone({ nodes, managed, external, managedEvents, externalEvents }); let committed = false, released = false
const restore = () => { for (const [target, original] of [[nodes, before.nodes], [managed, before.managed], [external, before.external], [managedEvents, before.managedEvents], [externalEvents, before.externalEvents]]) target.splice(0, target.length, ...original) }
const close = () => { if (released) return; released = true; closes++; if (!committed) restore() }
return { close, async binding() { await hook?.("binding"); return { ...root, issuerKeySha256: badBinding ? "e".repeat(64) : root.issuerKeySha256 } },
async keyedProof(purpose, value) { purposes.push(purpose); await hook?.("proof:" + purpose); const digest=mac(purpose,value);signedMaterials.push({purpose,value:structuredClone(value) as Row,mac:digest});return digest },
...(atomic?{async keyedProofs(inputs:readonly {purpose:string;value:unknown}[]){
batches++;const values=inputs.map(({purpose,value})=>{purposes.push(purpose);const digest=mac(purpose,value);signedMaterials.push({purpose,value:structuredClone(value) as Row,mac:digest});return digest})
// Issuer is modeled here; genuine atomic source is tested separately.
// Preserve revocation injection before returning the complete result.
for(const {purpose}of inputs)await hook?.("proof:"+purpose)
return Object.freeze(values)
}}:{}),
connection: { release: close, async query(sql, values = []) {
await dispatchGuard(); queries.push(sql); await hook?.("sql:" + sql)
const rows = (value: Row[] = [], command?: string) => ({ rows: structuredClone(value), command })
if (sql === "BEGIN") return rows([], "BEGIN")
if (sql === "ROLLBACK") { restore(); return rows([], "ROLLBACK") }
if (sql === "COMMIT") { committed = true; if (commitMode === "lost") throw Error("Synthetic lost COMMIT ACK"); if (commitMode === "hold") await new Promise(() => {}); return rows([], commitMode === "rows" ? undefined : "COMMIT") }
if (sql.includes("set_config") || sql.includes("pg_advisory_xact_lock")) return rows()
if (sql.includes("FROM mmapp_control.bandwidth_node_authority")) return rows([{ installation_id: root.installationId, issuer_key_sha256: root.issuerKeySha256, quiescence_proof: root.quiescenceProof, status: "active" }])
const isExternal = sql.includes("bandwidth_external_node_"), aliases = isExternal ? external : managed, events = isExternal ? externalEvents : managedEvents, id = isExternal ? "enrollment_id" : "server_id"
if(sql.startsWith("WITH aliases AS MATERIALIZED")){
const selected=aliases.filter(row=>row[id]===values[0])
return rows([{aliases:selected.map(row=>({subject:String(row[id]),incarnation:row.incarnation,node_id:row.node_id,status:row.status,record_mac:row.record_mac})),nodes:nodes.filter(row=>selected.some(alias=>alias.node_id===row.node_id)),events:events.filter(row=>row[id]===values[0]).map(row=>({event_id:row.event_id,incarnation:row.incarnation,node_id:row.node_id,kind:row.kind,old_status:row.old_status,new_status:row.new_status,authority_proof:row.authority_proof,event_mac:row.event_mac})),claim_nodes:values[1]===null?[]:nodes.filter(row=>row.claim_proof===values[1])}],"SELECT")
}
if(sql.startsWith("DO $mmapp_bandwidth_register$")){
// Model only the candidate's actual atomic DO boundary from captured
// signed material. No SQL parser or synthetic metadata guard replay.
const last=(purpose:string)=>{const value=[...signedMaterials].reverse().find(value=>value.purpose===purpose);assert(value);return value!}
const node=last("node"),alias=last(isExternal?"external-alias":"alias"),event=last(isExternal?"external-event":"event"),subject=isExternal?alias.value.enrollmentId:alias.value.serverId
for(const value of [node.value.nodeId,node.value.claimProof,node.mac,alias.value.incarnation,alias.mac,event.value.eventId,event.mac])assert(sql.includes(String(value)))
if(sql.includes("IF true THEN")){assert(!nodes.some(row=>row.claim_proof===node.value.claimProof));nodes.push({node_id:node.value.nodeId,claim_proof:node.value.claimProof,enrollment_mac:node.mac})}
else assert(nodes.some(row=>row.node_id===node.value.nodeId&&row.claim_proof===node.value.claimProof&&row.enrollment_mac===node.mac))
assert(!aliases.some(row=>row[id]===subject));aliases.push({[id]:subject,incarnation:alias.value.incarnation,node_id:alias.value.nodeId,status:"active",record_mac:alias.mac})
events.push({event_id:event.value.eventId,[id]:subject,incarnation:event.value.incarnation,node_id:event.value.nodeId,kind:"enroll",old_status:null,new_status:"active",authority_proof:event.value.authorityProof,event_mac:event.mac})
await hook?.("batch-applied")
return rows([],"DO")
}
if (sql.startsWith("SELECT") && sql.includes("FROM mmapp_control.bandwidth_nodes")) return rows(nodes.filter(row => row[sql.includes("WHERE claim_proof") ? "claim_proof" : "node_id"] === values[0]))
if (sql.startsWith("INSERT INTO mmapp_control.bandwidth_nodes")) { const row = { node_id: values[0], claim_proof: values[1], enrollment_mac: values[2] }; nodes.push(row); return rows([{ node_id: row.node_id }]) }
if (sql.startsWith("SELECT") && sql.includes("_aliases WHERE")) return rows(aliases.filter(row => row[id] === values[0]))
if (sql.startsWith("SELECT") && sql.includes("_events WHERE")) return rows(events.filter(row => row[id] === values[0]).sort((a, b) => String(a.kind).localeCompare(String(b.kind))))
if (sql.startsWith("INSERT") && sql.includes("_aliases(")) { const row = { [id]: values[0], incarnation: values[1], node_id: values[2], status: "active", record_mac: values[3] }; aliases.push(row); return rows([{ [id]: row[id] }]) }
if (sql.startsWith("INSERT") && sql.includes("_events(")) { events.push({ event_id: values[0], [id]: values[1], incarnation: values[2], node_id: values[3], kind: values[4], old_status: values[5], new_status: values[6], authority_proof: values[7], event_mac: values[8] }); return rows([{ event_id: values[0] }]) }
throw Error("Unexpected actual registry SQL: " + sql)
} },
}
} }
const dependencies = { coordinator, source }, host = createBandwidthOperatorRegistry(dependencies)
async function prerequisite(peerRef: BandwidthPeerRef, ms = 4000) {
const expiresAt = Date.now() + ms, routingEndpointEpoch = randomUUID()
currentSource = { peerRef, enabled: true, sourceProof: "c".repeat(64), authorityGeneration: "original-source", endpoint: {
host: "127.0.0.1", port, useSsl: false, verifySsl: true, username: "SYNTHETIC-OPERATOR", password: "SYNTHETIC-PASSWORD", routingEndpointEpoch } }
await publish(bandwidthOperatorEnrollmentGrantSchema.parse({ format: "mmapp-bandwidth-operator-enrollment-v1", ...root, grantId: randomUUID(), operatorId: "synthetic-original-operator",
executorInventorySha256: "e".repeat(64), executorsStoppedAt: Date.now() - 2000, issuedAt: Date.now() - 1000, expiresAt,
peerRef, sourceProof: currentSource.sourceProof, authorityGeneration: currentSource.authorityGeneration, routingEndpointEpoch, claims: [{ kind: "system-id", value: "SYNTHETIC-SHARED-CLAIM" }] }))
return coordinator.acquire(peerRef, scope(ms))
}
return { host, coordinator, dependencies, source, prerequisite, nodes, managed, external, managedEvents, externalEvents, queries, purposes, occupancy,
stats: () => ({ opens, closes, inspections, batches }), revoke: () => { revoked = true }, renew: async () => { assert(lastGrant); await publish({ ...lastGrant, expiresAt: lastGrant.expiresAt + 1000 }) }, badBinding: () => { badBinding = true },
hook: (value: typeof hook) => { hook = value }, openHook: (value: typeof openHook) => { openHook = value }, commit: (value: typeof commitMode) => { commitMode = value } }
}
test("explicit managed/external registration runs actual factories, shares canonical node, preserves unknown", async () => {
const f = await fixture(), initial = f.stats(); assert.equal(initial.opens, 0); assert.equal(initial.inspections, 0)
assert.deepEqual(Object.keys(f.host), ["register"])
const managedToken = await f.prerequisite({ kind: "server", serverId: 37 })
const first = await f.host.register(managedToken, scope()), externalToken = await f.prerequisite({ kind: "external", enrollmentId: randomUUID() })
const second = await f.host.register(externalToken, scope())
assert.equal(first.canonicalNodeId, second.canonicalNodeId); assert.notEqual(first.incarnation, second.incarnation)
assert.equal(f.nodes.length, 1); assert.equal(f.managed.length, 1); assert.equal(f.external.length, 1)
assert.equal((await f.host.register(externalToken, scope())).outcome, "existing"); assert.equal(f.externalEvents.length, 1)
assert(f.purposes.includes("alias")); assert(f.purposes.includes("external-alias")); assert(f.purposes.includes("external-event"))
assert(!f.queries.some(sql => /occupancy|bandwidth_attempt/.test(sql))); assert.equal(f.occupancy.state, "unknown")
assert.equal(f.queries.filter(sql => sql === "COMMIT").length, 3); assert.deepEqual(f.stats().opens, f.stats().closes)
})
test("captured fixed dependencies cannot be replaced; fake prerequisite fails before lease", async () => {
const f = await fixture(), token = await f.prerequisite({ kind: "server", serverId: 12 })
f.dependencies.coordinator = { ...f.coordinator }
assert.throws(() => createBandwidthOperatorRegistry(f.dependencies))
f.source.open = async () => { throw Error("Replacement source") }
await assert.rejects(f.host.register({ ...token }, scope())); assert.equal(f.stats().opens, 0)
assert.equal((await f.host.register(token, scope())).status, "active")
})
test("scalar-only trusted source remains compatible with the original guard path", async () => {
const f=await fixture(false),token=await f.prerequisite({kind:"external",enrollmentId:randomUUID()})
assert.equal((await f.host.register(token,scope())).status,"active")
assert.equal(f.stats().batches,0);assert.equal(f.externalEvents.length,1)
assert.deepEqual(f.purposes,["claim","node","external-alias","external-event"])
})
test("source binding drift and original grant renewal/revocation refuse without commit", async () => {
for (const change of ["badBinding", "renew", "revoke"] as const) {
const f = await fixture(), token = await f.prerequisite({ kind: "external", enrollmentId: randomUUID() })
if (change === "badBinding") f.badBinding()
else f.hook(async stage => { if (stage === "binding") await f[change]() })
await assert.rejects(f.host.register(token, scope())); assert.equal(f.queries.includes("COMMIT"), false); assert.equal(f.nodes.length, 0)
}
})
test("original quiescence/authority loss during issuer MAC rolls back actual factory writes", async () => {
for (const peerRef of [{ kind: "server", serverId: 21 }, { kind: "external", enrollmentId: randomUUID() }] as BandwidthPeerRef[])
for (const boundary of ["event-proof", "after-atomic-batch"] as const) {
const f = await fixture(), token = await f.prerequisite(peerRef)
f.hook(stage => { if (boundary === "event-proof" ? stage === "proof:event" || stage === "proof:external-event" : stage === "batch-applied") f.revoke() })
await assert.rejects(f.host.register(token, scope())); assert.equal(f.queries.includes("COMMIT"), false)
assert.equal(f.queries.filter(sql=>sql.startsWith("DO $mmapp_bandwidth_register$")).length,boundary==="after-atomic-batch"?1:0)
assert.equal(f.nodes.length, 0); assert.equal(f.managed.length, 0); assert.equal(f.external.length, 0)
}
})
test("no original-expiry renewal, cancelled setup destroys late lease, and no SQL follows", async () => {
const f = await fixture(), token = await f.prerequisite({ kind: "server", serverId: 42 }, 350)
f.openHook(() => new Promise(done => setTimeout(done, 500)))
await assert.rejects(f.host.register(token, scope(1000)))
await new Promise(done => setTimeout(done, 550)); assert.equal(f.stats().closes, 1); assert.equal(f.queries.length, 0)
})
test("lost/rows-only COMMIT ACK and held COMMIT remain unknown with no replay", async () => {
for (const peerRef of [{ kind: "server", serverId: 48 }, { kind: "external", enrollmentId: randomUUID() }] as BandwidthPeerRef[])
for (const mode of ["lost", "rows", "hold"] as const) {
// Actual signed-file guards take roughly a second on this filesystem. Use
// the existing four-second fixture budget to reach COMMIT; never renew it.
const f = await fixture(), token = await f.prerequisite(peerRef, 4000)
f.commit(mode)
await assert.rejects(f.host.register(token, scope()), error => !!error && typeof error === "object" && "code" in error && String(error.code).endsWith("UNKNOWN") && "automaticReplay" in error && error.automaticReplay === false)
assert.equal(f.external.length + f.managed.length, 1); assert.equal(f.externalEvents.length + f.managedEvents.length, 1); assert.equal(f.queries.filter(sql => sql === "COMMIT").length, 1)
assert(!f.queries.includes("ROLLBACK")); assert.equal(f.occupancy.state, "unknown")
}
})
test("original authority loss after COMMIT ACK is UNKNOWN for both real registry factories", async () => {
for (const peerRef of [{ kind: "server", serverId: 49 }, { kind: "external", enrollmentId: randomUUID() }] as BandwidthPeerRef[]) {
const f = await fixture(), token = await f.prerequisite(peerRef)
f.hook(stage => { if (stage === "sql:COMMIT") f.revoke() })
await assert.rejects(f.host.register(token, scope()), error => !!error && typeof error === "object" && "code" in error && String(error.code).endsWith("UNKNOWN"))
assert.equal(f.nodes.length, 1); assert.equal(f.managed.length + f.external.length, 1); assert(!f.queries.includes("ROLLBACK"))
}
})