Files
MikrotikManager/backend/src/modules/mcp/ipsec-revision-acceptance.ts
T

108 lines
16 KiB
TypeScript

import assert from "node:assert/strict"
import { installLoopbackFixtureAgent } from "../../test/loopback-http-agent.js"
import { randomUUID } from "node:crypto"
import { createServer } from "node:http"
import { setTimeout as delay } from "node:timers/promises"
import Fastify from "fastify"
import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod"
import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp"
import { pool } from "../../db/index.js"
import type { AuthUser } from "../../lib/permissions.js"
import { createIpsecRoutes } from "../../routes/ipsec.js"
import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js"
import { ipsecExternalPoolDependencyPaths } from "../../services/ipsec-pool-dependencies.js"
import { canonicalIpsecSnapshot } from "../../services/entity-snapshots.js"
import { appendRevisionIfChanged, fingerprintPayload } from "../../services/config-revisions.js"
import { ipsecManagedComment } from "../../services/ipsec-config.js"
import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js"
import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js"
/** Opt-in serial disposable SQL + loopback acceptance. No live RouterOS. */
export async function acceptIpsecRevisionRestore(actor: AuthUser) {
type Row = Record<string, string>
const serverId = 209, query = pool.query.bind(pool), comment = ipsecManagedComment("restore")
const base: Record<string, Row[]> = {
...Object.fromEntries([...ipsecPeerDeletePaths, ...ipsecExternalPoolDependencyPaths, "/ip/route", "/interface", "/interface/list", "/interface/list/member"].map(path => [path, []])),
"/ip/ipsec/peer": [{ ".id": "*1", name: "owned-peer", address: "0.0.0.0/0", "exchange-mode": "ike2", profile: "profile", passive: "true", disabled: "false", comment, opaque: "NEVER_PEER" }],
"/ip/ipsec/identity": [{ ".id": "*2", peer: "owned-peer", "auth-method": "pre-shared-key", secret: "NEVER_PSK", "remote-id": "old-id", "mode-config": "ipsec-vpn", comment, disabled: "false", opaque: "NEVER_IDENTITY" }, { ".id": "*3", peer: "owned-peer", "auth-method": "digital-signature", certificate: "server-cert", "mode-config": "ipsec-vpn", "generate-policy": "port-strict", "policy-template-group": "group", comment: ipsecManagedComment("cert"), disabled: "false" }],
"/ip/ipsec/mode-config": [{ ".id": "*4", name: "ipsec-vpn", "address-pool": "pool", responder: "true", "static-dns": "192.0.2.53", opaque: "NEVER_MC" }],
"/ip/pool": [{ ".id": "*5", name: "pool", ranges: "10.77.0.2-10.77.0.254", comment, opaque: "NEVER_POOL" }],
"/ip/ipsec/policy": [{ ".id": "*6", template: "true", group: "group", "src-address": "0.0.0.0/0", "dst-address": "10.77.0.0/24", proposal: "proposal", comment, opaque: "NEVER_POLICY" }],
"/ip/firewall/nat": [{ ".id": "*7", chain: "srcnat", action: "masquerade", "src-address": "10.77.0.0/24", "out-interface-list": "WAN", "ipsec-policy": "out,none", comment, opaque: "NEVER_NAT" }],
"/ip/ipsec/profile": [{ ".id": "*8", name: "profile", opaque: "NEVER_PROFILE" }], "/ip/ipsec/proposal": [{ ".id": "*9", name: "proposal", opaque: "NEVER_PROPOSAL" }], "/ip/ipsec/policy/group": [{ ".id": "*a", name: "group", opaque: "NEVER_GROUP" }],
"/certificate": [{ ".id": "*b", name: "ca", fingerprint: "signed-ca", "invalid-after": "2030-01-01" }, { ".id": "*c", name: "server-cert", ca: "ca", fingerprint: "signed", "invalid-after": "2030-01-01", "private-key": "true", opaque: "NEVER_CERT" }],
"/interface": [{ ".id": "*d", name: "ether1" }], "/interface/list": [{ ".id": "*e", name: "WAN" }],
}
const desired = canonicalIpsecSnapshot({ peers: [{ name: "owned-peer", address: "", exchangeMode: "ike2", profile: "profile", passive: true, disabled: false, comment }], identities: [{ peerName: "owned-peer", authMethod: "pre-shared-key", secret: "(hidden)", modeConfig: "ipsec-vpn", comment }, { peerName: "owned-peer", authMethod: "digital-signature", certificate: "server-cert", modeConfig: "ipsec-vpn", generatePolicy: "port-strict", policyTemplateGroup: "group", comment: ipsecManagedComment("cert") }], modeConfigs: [{ name: "ipsec-vpn", addressPool: "pool", staticDns: "" }], pools: [{ name: "pool", ranges: "10.77.0.2-10.77.0.200", comment }], policies: [{ srcAddress: "0.0.0.0/0", dstAddress: "10.78.0.0/24", proposal: "proposal", comment }], nat: [{ chain: "srcnat", action: "masquerade", srcAddress: "10.78.0.0/24", outInterface: "ether1", comment }] })
let catalogs = structuredClone(base), serial = 0, mode = "normal", connectionId: string | undefined, afterAck: (() => Promise<unknown>) | undefined, revisionId = ""
const commands: Array<{ method: string; path: string; fields: Row }> = []
const router = createServer(async (req, res) => {
const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")); res.setHeader("content-type", "application/json")
try {
if (req.method === "GET") { res.end(JSON.stringify(catalogs[path] ?? [])); return }
let raw = ""; for await (const chunk of req) raw += chunk
const fields = raw ? JSON.parse(raw) as Row : {}; commands.push({ method: req.method!, path, fields })
const catalog = Object.hasOwn(catalogs, path) ? path : Object.keys(catalogs).sort((a, b) => b.length - a.length).find(value => path.startsWith(value + "/")); assert(catalog)
let result: Row = {}
if (mode !== "ignored") {
if (req.method === "PUT") { result = { ".id": `*new${++serial}`, ...fields, "native-default": "NEVER_NEW_DEFAULT", ...(mode === "recordedDefault" && catalog === "/ip/ipsec/identity" ? { "remote-id": "auto" } : {}) }; catalogs[catalog].push(result) }
else if (req.method === "PATCH") { const row = catalogs[catalog].find(row => row[".id"] === path.slice(catalog.length + 1)); assert(row); Object.assign(row, fields); if (mode === "omitClears") for (const [key, value] of Object.entries(fields)) if (value === "") delete row[key] }
else if (req.method === "DELETE") catalogs[catalog] = catalogs[catalog].filter(row => row[".id"] !== path.slice(catalog.length + 1))
else throw Error("unsupported mutation")
}
if (mode === "collateral") catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_COLLATERAL"
if (afterAck) { const run = afterAck; afterAck = undefined; await run() }
if (mode === "lost") { req.socket.destroy(); return }
if (mode === "missingAck" && req.method === "PUT") result = {}
res.end(JSON.stringify(result))
} catch { res.statusCode = 500; res.end('{"error":"NEVER_PROVIDER"}') }
})
await new Promise<void>(resolve => router.listen(0, "127.0.0.1", resolve))
const closeFixtureAgent = installLoopbackFixtureAgent(router)
const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler)
const reset = () => { catalogs = structuredClone(base); serial = 0; mode = "normal"; afterAck = undefined }
const hidden = (value: unknown) => assert(!JSON.stringify(value).includes("NEVER_"), "private revision or raw state leaked")
const wait = async (id: string) => { for (const deadline = Date.now() + 140000; Date.now() < deadline;) { const row = (await query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (!["pending", "running"].includes(row.state)) return row; await delay(25) }; throw Error("Restore active; never replay") }
try {
assert.equal(Number((await query("SELECT count(*)::text AS n FROM servers WHERE id=$1", [serverId])).rows[0].n), 0)
await query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'ipsec-restore','127.0.0.1',$2,false,'NEVER_ENDPOINT')", [serverId, (router.address() as { port: number }).port])
await rest.register(createIpsecRoutes(), { prefix: "/api" })
const save = async (payload = desired) => { revisionId = (await appendRevisionIfChanged({ serverId, section: "ipsec", source: "copy", payload: canonicalIpsecSnapshot(payload) })).revision.id; return revisionId }
await save()
const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "IPsec revision restore", mode: "autonomous", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: ["network.ipsec.revisions.restore", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor)
connectionId = access.connection.id; const principal = await authenticateMcpToken(access.token)
const prepare = () => invokeMcpTool(principal, "mm_ipsec_revision_restore_plan", { serverId, revisionId }) as Promise<{ planId: string; state: string }>
const apply = async () => { const plan = await prepare(); hidden(plan); assert.equal(plan.state, "awaiting-approval"); await approvePlan(plan.planId, actor, true); const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key); return { plan, key, operation, ...await wait(operation.id) } }
const receipt = async (id: string) => (await query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [id])).rows[0].result
const normalize = (entries: typeof commands) => entries.map(row => ({ ...row, path: row.path.replace(/\*new\d+/g, "*created") }))
for (const clears of ["normal", "omitClears"]) {
reset(); mode = clears; await save(); const start = commands.length, result = await apply()
assert.equal(result.state, "applied", JSON.stringify({ result, receipt: await receipt(result.operation.id), native: commands.slice(start).map(row => ({ method: row.method, path: row.path, fieldNames: Object.keys(row.fields) })) })); hidden(result)
const accepted = normalize(commands.slice(start)); hidden(await receipt(result.operation.id)); assert.equal(catalogs["/ip/ipsec/identity"].find(row => row[".id"] === "*2")!.secret, "NEVER_PSK")
assert.equal(catalogs["/ip/ipsec/policy"][0]["dst-address"], "10.78.0.0/24"); assert.equal(catalogs["/ip/ipsec/policy"][0].opaque, "NEVER_POLICY")
for (const path of ["/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group"]) assert.deepEqual(catalogs[path], base[path])
reset(); mode = clears; const restStart = commands.length, response = await rest.inject({ method: "POST", url: `/api/ipsec/revisions/${revisionId}/restore`, payload: { serverId } }); assert.equal(response.statusCode, 200, response.body); assert.deepEqual(normalize(commands.slice(restStart)), accepted)
}
reset(); await save(canonicalIpsecSnapshot({})); const deletion = await apply(); assert.equal(deletion.state, "applied"); for (const path of ["/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat"]) assert.deepEqual(catalogs[path], [])
const withNewPool = { ...desired, pools: [...desired.pools, { name: "new-pool", ranges: "192.0.2.2-192.0.2.10", comment: ipsecManagedComment("new-pool") }] }
reset(); await save(withNewPool); const created = await apply(); assert.equal(created.state, "applied"); assert.equal(catalogs["/ip/pool"].find(row => row.name === "new-pool")!["native-default"], "NEVER_NEW_DEFAULT")
reset(); mode = "missingAck"; const ackStart = commands.length, missingAck = await apply(); assert(["partial", "unknown"].includes(missingAck.state)); assert.equal(commands[ackStart].method, "PUT"); assert.equal(commands[ackStart].fields.name, "new-pool"); assert.equal(commands.length, ackStart + 1); await applyPlan(principal, missingAck.plan.planId, missingAck.key); assert.equal(commands.length, ackStart + 1)
reset(); await save({ ...desired, identities: [...desired.identities, { ...desired.identities.find(row => row.authMethod === "pre-shared-key")!, secret: "NEVER_NEW_PSK", comment: ipsecManagedComment("new-explicit") }] }); mode = "recordedDefault"
const defaultStart = commands.length, defaultResult = await apply(); assert(["partial", "unknown"].includes(defaultResult.state)); const defaultDispatches = commands.slice(defaultStart); assert.equal(defaultDispatches.at(-1)!.method, "PUT"); assert.equal(defaultDispatches.at(-1)!.path, "/ip/ipsec/identity"); assert.equal(defaultDispatches.filter(row => row.path === "/ip/firewall/nat/*7").length, 0); const defaultStoppedAt = commands.length; await applyPlan(principal, defaultResult.plan.planId, defaultResult.key); assert.equal(commands.length, defaultStoppedAt)
reset(); await save(); const start = commands.length, unapproved = await prepare(); await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(commands.length, start)
for (const path of ["/ip/ipsec/profile", "/certificate", ...ipsecExternalPoolDependencyPaths, "/interface/list"]) { reset(); const stale = await prepare(); await approvePlan(stale.planId, actor, true); catalogs[path].push({ ".id": "*late", name: "late", opaque: "NEVER_LATE" }); assert.notEqual((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, start) }
reset(); const staleRevision = await prepare(); await approvePlan(staleRevision.planId, actor, true); await query("UPDATE config_revisions SET note='changed' WHERE id=$1", [revisionId]); assert.notEqual((await wait((await applyPlan(principal, staleRevision.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, start)
for (const mutate of [() => { catalogs["/certificate"] = [] }, () => { catalogs["/certificate"][1]["private-key"] = "false" }, () => { catalogs["/ip/ipsec/profile"] = [] }, () => { catalogs["/ip/ipsec/proposal"] = [] }, () => { catalogs["/ip/ipsec/policy/group"] = [] }, () => { catalogs["/ip/ipsec/policy"] = [] }, () => { catalogs["/ip/ipsec/policy"].push({ ".id": "*consumer", comment: "foreign", peer: "other,*0001" }) }]) { reset(); mutate(); await assert.rejects(prepare()); assert.equal(commands.length, start) }
for (const path of ipsecExternalPoolDependencyPaths) { reset(); catalogs[path].push({ ".id": "*allocator", [path.startsWith("/ppp/") ? "remote-address" : "address-pool"]: "*0005" }); await assert.rejects(prepare()); assert.equal(commands.length, start) }
reset(); await query("UPDATE config_revisions SET fingerprint=$2 WHERE id=$1", [revisionId, "0".repeat(64)]); await assert.rejects(prepare()); await query("UPDATE config_revisions SET fingerprint=$2 WHERE id=$1", [revisionId, fingerprintPayload(desired)])
for (const fault of ["lost", "ignored", "collateral"]) { reset(); mode = fault; const begin = commands.length, result = await apply(); assert(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, begin + 1); await applyPlan(principal, result.plan.planId, result.key); assert.equal(commands.length, begin + 1); const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean }[] }; assert.equal(reconciliation.automaticRetry, false); assert.equal(reconciliation.results[0].matchesDesiredState, false); assert.equal(commands.length, begin + 1) }
reset(); const begin = commands.length; afterAck = () => setConnectionStatus(connectionId!, "paused", actor); const revoked = await apply(); assert(["partial", "unknown"].includes(revoked.state)); assert.equal(commands.length, begin + 1)
for (const table of ["mcp_audit", "mcp_plans", "mcp_operation_steps"]) hidden((await query(table === "mcp_operation_steps" ? "SELECT s.* FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1" : `SELECT * FROM ${table} WHERE connection_id=$1`, [connectionId])).rows)
console.log("✓ IPsec revision SQL/loopback: full recorded fields/policies and exact clears, managed deletion, REST parity, revision fingerprint/xmin drift, hidden PSK preserve, PKI/dependency/pool foreign guards, loss/collateral/ignored no replay, revoke and private audit")
} finally {
closeFixtureAgent()
if (connectionId) await setConnectionStatus(connectionId, "paused", actor)
await rest.close(); await query("DELETE FROM servers WHERE id=$1", [serverId]); router.closeAllConnections(); await new Promise<void>(resolve => router.close(() => resolve()))
}
}