120 lines
8.3 KiB
TypeScript
120 lines
8.3 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import test from "node:test"
|
|
import { readVrfProductionSnapshot, VRF_PRODUCTION_RAW_PATHS, type ReadVrfProductionInput } from "./vrf-production-read.js"
|
|
import { sealVrfProductionAdmission, inspectVrfProductionAdmission, VRF_PRODUCTION_REQUIRED_CATALOGS, type VrfProductionSnapshot } from "./vrf-production-admission.js"
|
|
import { vrfProductionDependencyObjects } from "./vrf-production-transition.js"
|
|
|
|
const marker = "mcp-route-fea781dcfaab", wg = marker + "-wg", gre = marker + "-gre-a"
|
|
const proof = "a".repeat(64), secret = Buffer.alloc(32, 73), now = 100000
|
|
const binding = { serverId: 1, sourceProof: proof, actorId: "fixture-owner", planId: "fixture-vrf-plan" }
|
|
|
|
// Reduced configuration from the V23 safe snapshot: GRE -> static outer IPv4 ->
|
|
// RouterOS /interface type "wg". Original native IDs and raw type stay intact.
|
|
function model(type = "wg") {
|
|
const catalogs: VrfProductionSnapshot["catalogs"] = Object.fromEntries(VRF_PRODUCTION_REQUIRED_CATALOGS.map(resource => [resource, []]))
|
|
catalogs.ip_settings = [{ "rp-filter": "loose" }]
|
|
catalogs.ipv6_settings = [{ "disable-ipv6": "false" }]
|
|
catalogs.vrf = [{ ".id": "*0", name: "main", interfaces: "all", builtin: "true", disabled: "false" }]
|
|
catalogs.routing_table = [{ ".id": "*0", name: "main", fib: "true" }]
|
|
catalogs.interface_inventory = [{ ".id": "*1B", name: wg, type, disabled: "false" },
|
|
{ ".id": "*1C", name: gre, type: "gre-tunnel", disabled: "false" }]
|
|
catalogs.wireguard = [{ ".id": "*1B", name: wg, "listen-port": "55100", mtu: "1420", disabled: "false", comment: marker }]
|
|
catalogs.wireguard_peer = [{ ".id": "*3", interface: wg, "endpoint-address": "192.0.2.7", "endpoint-port": "55101",
|
|
"allowed-address": "198.18.245.10/32,198.18.245.14/32", "persistent-keepalive": "10s", disabled: "false", comment: marker }]
|
|
catalogs.gre = [{ ".id": "*1C", name: gre, "local-address": "198.18.245.9", "remote-address": "198.18.245.10",
|
|
"allow-fast-path": "false", "clamp-tcp-mss": "true", disabled: "false", comment: marker }]
|
|
catalogs.ipv4_address = [{ ".id": "*1F", interface: wg, address: "198.18.245.9/30", dynamic: "false", disabled: "false", vrf: "main", comment: marker },
|
|
{ ".id": "*21", interface: gre, address: "198.18.245.1/30", dynamic: "false", disabled: "false", vrf: "main", comment: marker }]
|
|
catalogs.ipv4_route = [{ ".id": "*30", "dst-address": "198.18.245.0/30", dynamic: "true", connect: "true", "routing-table": "main", gateway: gre, "immediate-gw": gre }]
|
|
let enabled = true, sourceProof = proof, revoked = false
|
|
let afterGet = (_path: string) => {}
|
|
const calls: string[] = []
|
|
const input: ReadVrfProductionInput = { serverId: 1, expectedSourceProof: proof,
|
|
request: { action: "create", placeBeforeId: "*0", fields: { name: marker + "-vrf", interfaces: gre, comment: marker } },
|
|
async assertAuthority() { assert(!revoked, "Owner revoked") },
|
|
adapter: { async resolve() { return { serverId: 1, enabled, sourceProof, client: { async get<T>(path: string) {
|
|
calls.push(path)
|
|
const resource = Object.keys(VRF_PRODUCTION_RAW_PATHS).find(key => VRF_PRODUCTION_RAW_PATHS[key] === path)
|
|
assert(resource && Object.hasOwn(catalogs, resource), "Unexpected raw read")
|
|
const result = structuredClone(catalogs[resource])
|
|
afterGet(path)
|
|
return result as T
|
|
} } } } },
|
|
}
|
|
return { catalogs, input, calls, revoke() { revoked = true }, disable() { enabled = false },
|
|
changeSource() { sourceProof = "b".repeat(64) }, afterGet(callback: (path: string) => void) { afterGet = callback } }
|
|
}
|
|
|
|
for (const type of ["wg", "wireguard"]) test(`reads and seals GRE through static outer IPv4 and ${type} parent without changing native identity`, async () => {
|
|
const m = model(type)
|
|
const snapshot = await readVrfProductionSnapshot(m.input)
|
|
assert.equal(snapshot.catalogs.interface_inventory[0].type, type)
|
|
assert(m.calls.includes("/interface/wireguard/peers"))
|
|
const issued = sealVrfProductionAdmission({ snapshot, request: m.input.request, binding, secret, now })
|
|
const checked = inspectVrfProductionAdmission({ sealed: issued.sealed, snapshot, request: m.input.request, binding, secret, now: now + 1 })
|
|
assert.deepEqual(checked.admission.members[0].parents.map(parent => [parent.id, parent.resource, parent.configurationId]),
|
|
[["*1C", "gre", "*1C"], ["*1B", "wireguard", "*1B"]])
|
|
assert.equal(checked.admission.members[0].addresses.length, 2)
|
|
assert.deepEqual(vrfProductionDependencyObjects(snapshot, checked.admission).map(object => [object.kind, object.id]), [
|
|
["gre", "*1C"], ["interface_inventory", "*1C"], ["wireguard", "*1B"], ["interface_inventory", "*1B"],
|
|
["ipv4_address", "*1F"], ["ipv4_address", "*21"], ["ipv4_route", "*30"],
|
|
])
|
|
})
|
|
|
|
// Synthetic provenance and route evidence close the layer that alias-only
|
|
// reader/admission tests previously skipped; they do not establish a live cause.
|
|
test("alias dependency closure refuses missing static provenance and exact connected route evidence", async () => {
|
|
for (const mutation of ["absent-vrf", "absent-dynamic", "missing-route", "immediate-gw-mismatch"]) {
|
|
const m = model()
|
|
const address = m.catalogs.ipv4_address[1]
|
|
if (mutation === "absent-vrf") delete address.vrf
|
|
if (mutation === "absent-dynamic") delete address.dynamic
|
|
if (mutation === "missing-route") m.catalogs.ipv4_route = []
|
|
if (mutation === "immediate-gw-mismatch") m.catalogs.ipv4_route[0]["immediate-gw"] = wg
|
|
const snapshot = await readVrfProductionSnapshot(m.input)
|
|
const issued = sealVrfProductionAdmission({ snapshot, request: m.input.request, binding, secret, now })
|
|
assert.throws(() => vrfProductionDependencyObjects(snapshot, issued.admission),
|
|
mutation.startsWith("absent-") ? /static address provenance\/context/ : /dependency connected route identity/)
|
|
}
|
|
})
|
|
|
|
test("unsupported parent types and ambiguous or dynamic tunnel source parents still refuse", async () => {
|
|
for (const type of ["wg-unknown", "__proto__", "constructor"]) {
|
|
const m = model(type)
|
|
await assert.rejects(readVrfProductionSnapshot(m.input), /unsupported\/missing concrete/)
|
|
}
|
|
for (const mutation of ["duplicate-address", "dynamic-address", "duplicate-inventory", "unresolved-peer"]) {
|
|
const m = model()
|
|
if (mutation === "duplicate-address") m.catalogs.ipv4_address.push({ ...m.catalogs.ipv4_address[0], ".id": "*99" })
|
|
if (mutation === "dynamic-address") m.catalogs.ipv4_address[0].dynamic = "true"
|
|
if (mutation === "duplicate-inventory") m.catalogs.interface_inventory.push({ ...m.catalogs.interface_inventory[0], ".id": "*99" })
|
|
if (mutation === "unresolved-peer") m.catalogs.wireguard_peer[0]["endpoint-address"] = "peer.example.test"
|
|
await assert.rejects(readVrfProductionSnapshot(m.input), /VRF production read refused/)
|
|
}
|
|
})
|
|
|
|
test("authority revocation, disabled source and source drift still stop the aliased parent read", async () => {
|
|
for (const change of ["revoke", "disable", "changeSource"] as const) {
|
|
const m = model()
|
|
m.afterGet(path => { if (path === "/interface/wireguard") m[change]() })
|
|
await assert.rejects(readVrfProductionSnapshot(m.input))
|
|
assert.equal(m.calls.at(-1), "/interface/wireguard")
|
|
}
|
|
})
|
|
|
|
test("sealed alias admission still refuses stale tickets and peer or root configuration drift", async () => {
|
|
const m = model(), snapshot = await readVrfProductionSnapshot(m.input)
|
|
const issued = sealVrfProductionAdmission({ snapshot, request: m.input.request, binding, secret, now, ttlMs: 1000 })
|
|
const input = { sealed: issued.sealed, snapshot, request: m.input.request, binding, secret, now: now + 1 }
|
|
assert.throws(() => inspectVrfProductionAdmission({ ...input, now: now + 1000 }), /expired\/future ticket/)
|
|
for (const change of ["peer-disabled", "peer-allowed", "wg-disabled", "inventory-id", "gre-source"]) {
|
|
const changed = structuredClone(snapshot)
|
|
if (change === "peer-disabled") changed.catalogs.wireguard_peer[0].disabled = "true"
|
|
if (change === "peer-allowed") changed.catalogs.wireguard_peer[0]["allowed-address"] = "0.0.0.0/0"
|
|
if (change === "wg-disabled") changed.catalogs.wireguard[0].disabled = "true"
|
|
if (change === "inventory-id") changed.catalogs.interface_inventory[0][".id"] = "*99"
|
|
if (change === "gre-source") changed.catalogs.gre[0]["local-address"] = "198.18.245.13"
|
|
assert.throws(() => inspectVrfProductionAdmission({ ...input, snapshot: changed }), /VRF production admission refused/)
|
|
}
|
|
})
|