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

143 lines
18 KiB
TypeScript

import { ipsecExternalPoolDependencyPaths } from "../../services/ipsec-pool-dependencies.js"
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 { getEnabledIpsecServerById } from "../../services/ipsec-live.js"
import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js"
import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js"
import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js"
/** Owned PostgreSQL/loopback acceptance; actual RouterOS cascade and forwarding
* acceptance remain separate. The simulator enforces an explicit raw manifest. */
export async function acceptIpsecServerDelete(actor: AuthUser) {
type Row = Record<string, string>
const serverId = 186, originalQuery = pool.query.bind(pool), marker = "MikrotikManager:ipsec"
const base: Record<string, Row[]> = {
...Object.fromEntries([...ipsecPeerDeletePaths,...ipsecExternalPoolDependencyPaths].map(path => [path, []])),
"/ip/ipsec/identity": [
{ ".id": "*i1", peer: "owned-peer", "auth-method": "digital-signature", certificate: "ipsec-server", "remote-certificate": "ipsec-user-alice", "mode-config": "ipsec-vpn", "policy-template-group": "mmapp-ipsec-vpn", comment: `${marker} user=Alice`, secret: "NEVER_MANAGED_PSK" },
{ ".id": "*i2", peer: "foreign-peer", "auth-method": "pre-shared-key", "mode-config": "foreign-mc", "policy-template-group": "foreign-group", comment: "foreign identity", secret: "NEVER_FOREIGN_PSK", opaque: "NEVER_FOREIGN_IDENTITY" },
],
"/ip/ipsec/peer": [{ ".id": "*p1", name: "owned-peer", "exchange-mode": "ike2", profile: "profile", comment: marker }, { ".id": "*p2", name: "foreign-peer", profile: "profile", opaque: "NEVER_FOREIGN_PEER" }],
"/ip/ipsec/policy": [{ ".id": "*s1", peer: "owned-peer", group: "mmapp-ipsec-vpn", proposal: "proposal", dynamic: "false", comment: marker }, { ".id": "*s2", peer: "foreign-peer", group: "foreign-group", proposal: "proposal", dynamic: "false", opaque: "NEVER_FOREIGN_POLICY" }],
"/ip/ipsec/mode-config": [{ ".id": "*m1", name: "ipsec-vpn", "address-pool": "owned-pool" }, { ".id": "*m2", name: "foreign-mc", "address-pool": "foreign-pool", opaque: "NEVER_FOREIGN_MC" }],
"/ip/pool": [{ ".id": "*l1", name: "owned-pool", ranges: "192.0.2.1-192.0.2.20", comment: marker }, { ".id": "*l2", name: "foreign-pool", ranges: "198.51.100.1-198.51.100.20", opaque: "NEVER_FOREIGN_POOL" }],
"/ip/firewall/nat": [{ ".id": "*n1", chain: "srcnat", action: "accept", comment: marker }, { ".id": "*n2", chain: "srcnat", action: "masquerade", opaque: "NEVER_FOREIGN_NAT" }],
"/ip/ipsec/policy/group": [{ ".id": "*g1", name: "mmapp-ipsec-vpn" }, { ".id": "*g2", name: "foreign-group", opaque: "NEVER_FOREIGN_GROUP" }],
"/ip/ipsec/profile": [{ ".id": "*f1", name: "profile", comment: marker, opaque: "NEVER_RETAIN_PROFILE" }],
"/ip/ipsec/proposal": [{ ".id": "*r1", name: "proposal", comment: marker, opaque: "NEVER_RETAIN_PROPOSAL" }],
"/certificate": [{ ".id": "*c1", name: "ipsec-ca", authority: "true", "key-usage": "key-cert-sign,crl-sign", crl: "false" }, { ".id": "*c2", name: "ipsec-server", ca: "ipsec-ca", issued: "true", "key-usage": "tls-server", "private-key": "true" }, { ".id": "*c3", name: "ipsec-user-alice", ca: "*c1", issued: "true", "key-usage": "tls-client", opaque: "NEVER_CLIENT_CERT" }, { ".id": "*c4", name: "foreign-ca", authority: "true" }, { ".id": "*c5", name: "foreign-cert", ca: "foreign-ca", opaque: "NEVER_FOREIGN_CERT" }],
}
const managed: Record<string, string[]> = { "/ip/ipsec/identity": ["*i1"], "/ip/ipsec/peer": ["*p1"], "/ip/ipsec/policy": ["*s1"], "/ip/ipsec/mode-config": ["*m1"], "/ip/pool": ["*l1"], "/ip/firewall/nat": ["*n1"], "/ip/ipsec/policy/group": ["*g1"] }
let catalogs = structuredClone(base), mode = "normal", reads = 0, historyCalls = 0, connectionId: string | undefined, inserted = false
let afterAck: (() => Promise<unknown>) | undefined, onRead: (() => Promise<unknown>) | undefined, ticking = false, clock = 100000
const commands: string[] = []
const router = createServer(async (req, res) => {
const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")); res.setHeader("content-type", "application/json")
try {
if (req.method === "GET") { reads++; if (ticking) for (const row of catalogs["/certificate"]) row["expires-after"] = `${clock--}s`; if (onRead) { const hook = onRead; onRead = undefined; await hook() }; res.end(JSON.stringify(catalogs[path] ?? [])); return }
const catalog = Object.keys(catalogs).sort((a, b) => b.length - a.length).find(value => path.startsWith(value + "/"))
if (req.method !== "DELETE" || !catalog) { res.statusCode = 405; res.end("{}"); return }
commands.push(path)
if (mode !== "ignored") {
const id = path.slice(catalog.length + 1), target = catalogs[catalog].find(row => row[".id"] === id); assert.ok(target)
const removed = new Set([id])
if (catalog === "/certificate" && mode !== "cascade-root-only") {
const queue = [target]
for (let index = 0; index < queue.length; index++) for (const row of catalogs[catalog]) if (!removed.has(row[".id"]) && [queue[index].name, queue[index][".id"]].includes(row.ca)) { removed.add(row[".id"]); queue.push(row) }
if (mode === "cascade-extra" && id === "*c1") removed.add("*c5")
}
catalogs[catalog] = catalogs[catalog].filter(row => !removed.has(row[".id"]))
}
if (mode === "collateral") catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_COLLATERAL"
if (afterAck) { const hook = afterAck; afterAck = undefined; await hook() }
if (mode === "lost") { req.socket.destroy(); return }
res.end("{}")
} 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 = (crl = false) => { mode = "normal"; catalogs = structuredClone(base); if (crl) catalogs["/certificate"][0].crl = "true"; afterAck = undefined; onRead = undefined; ticking = false }
const wait = async (id: string) => { for (const deadline = Date.now() + 140000; Date.now() < deadline;) { const row = (await originalQuery("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("IPsec server removal remains live; never replay") }
const desired = (before: Record<string, Row[]>, removeCertificates: boolean) => Object.fromEntries(Object.entries(before).map(([path, rows]) => [path, rows.filter(row => !(managed[path] ?? []).includes(row[".id"]) && !(removeCertificates && path === "/certificate" && ["*c1", "*c2", "*c3"].includes(row[".id"])))]))
try {
assert.equal(Number((await originalQuery("SELECT count(*)::text AS n FROM servers WHERE id=$1", [serverId])).rows[0].n), 0)
await originalQuery("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'ipsec-server-delete','127.0.0.1',$2,false,'NEVER_ENDPOINT')", [serverId, (router.address() as { port: number }).port]); inserted = true
await rest.register(createIpsecRoutes({ findServer: getEnabledIpsecServerById, recordRevision: async input => { historyCalls++; assert.ok(await input.capture()); return { id: "fixture-history" } as never } }), { prefix: "/api" })
const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "IPsec server delete", mode: "autonomous", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: ["network.ipsec.server.delete", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor)
connectionId = access.connection.id
const principal = await authenticateMcpToken(access.token)
const prepare = (removeCertificates?: boolean) => invokeMcpTool(principal, "mm_ipsec_server_remove_plan", { serverId, ...(removeCertificates === undefined ? {} : { removeCertificates }) }) as Promise<{ planId: string; state: string }>
const apply = async (removeCertificates?: boolean) => { const plan = await prepare(removeCertificates); assert.equal(plan.state, "awaiting-approval"); assert.ok(!JSON.stringify(plan).includes("NEVER_")); 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 noReplay = async (result: Awaited<ReturnType<typeof apply>>) => {
const before = commands.length
const persisted = (await originalQuery("SELECT state FROM mcp_operation_steps WHERE operation_id=$1", [result.operation.id])).rows
assert.ok(persisted.every(row => row.state !== "verified"))
await applyPlan(principal, result.plan.planId, result.key); assert.equal(commands.length, before)
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, before)
}
const unapproved = await prepare(false); await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(commands.length, 0)
for (const scenario of [{ remove: false, crl: false }, { remove: true, crl: false }, { remove: true, crl: true }]) {
reset(scenario.crl); const original = structuredClone(catalogs), before: number = commands.length, result = await apply(scenario.remove)
assert.equal(result.state, "applied"); assert.deepEqual(catalogs, desired(original, scenario.remove)); assert.deepEqual(catalogs["/ip/ipsec/profile"], original["/ip/ipsec/profile"]); assert.deepEqual(catalogs["/ip/ipsec/proposal"], original["/ip/ipsec/proposal"])
const expectedNative = ["/ip/ipsec/identity/*i1", "/ip/ipsec/policy/*s1", "/ip/ipsec/peer/*p1", "/ip/ipsec/mode-config/*m1", "/ip/pool/*l1", "/ip/firewall/nat/*n1", "/ip/ipsec/policy/group/*g1"]
assert.deepEqual(commands.slice(before, before + 7), expectedNative)
if (scenario.crl) assert.deepEqual(commands.slice(before + 7), ["/certificate/*c1"])
else if (!scenario.remove) assert.equal(commands.length, before + 7)
else assert.deepEqual(new Set(commands.slice(before + 7)), new Set(["/certificate/*c1", "/certificate/*c2", "/certificate/*c3"]))
const receipt: Record<string, unknown> = (await originalQuery("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [result.operation.id])).rows[0].result; assert.equal(receipt.historyRecorded, true); assert.ok(!JSON.stringify(receipt).includes("NEVER_"))
const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { results: { matchesDesiredState: boolean }[] }; assert.equal(reconciliation.results[0].matchesDesiredState, true)
const successfulCommands = commands.slice(before); reset(scenario.crl); const restBefore: number = commands.length, historyBefore = historyCalls
const response = await rest.inject({ method: "DELETE", url: `/api/ipsec/server/${serverId}?removeCertificates=${scenario.remove}` }); assert.equal(response.statusCode, 200, response.body); assert.deepEqual(response.json(), { ok: true }); assert.deepEqual(commands.slice(restBefore), successfulCommands); assert.deepEqual(catalogs, desired(original, scenario.remove)); assert.equal(historyCalls, historyBefore + 1)
}
reset(); ticking = true
catalogs["/ip/ipsec/policy"].push({ ".id": "*runtime", dynamic: "true", peer: "vanished-runtime-peer", group: "vanished-runtime-group", comment: marker })
const withCountdown = await apply(false)
assert.equal(withCountdown.state, "applied", "documented expiry countdown must not invalidate the full manifest")
assert.equal(catalogs["/certificate"].length, base["/certificate"].length)
assert.ok(clock < 100000); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/server/${serverId}?removeCertificates=false` })).statusCode, 200)
reset(); let result = await apply(); assert.equal(result.state, "applied"); assert.equal(catalogs["/certificate"].length, 2)
let before = commands.length; result = await apply(); assert.equal(result.state, "applied"); assert.equal(commands.length, before); assert.equal((await originalQuery("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [result.operation.id])).rows[0].result.historyRecorded, true)
const noopHistory = historyCalls; assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/server/${serverId}` })).statusCode, 200); assert.equal(commands.length, before); assert.equal(historyCalls, noopHistory + 1)
const denied = [
...ipsecExternalPoolDependencyPaths.flatMap(path=>["owned-pool","*L1","*00l1"].map(ref=>()=>{catalogs[path].push({".id":"*consumer",name:"foreign",disabled:"true",...(path.startsWith("/ppp/")?{"remote-address":ref}:{"address-pool":ref})})})),
() => { catalogs["/ip/ipsec/mode-config"][1]["address-pool"] = "owned-pool" },
() => { catalogs["/ip/ipsec/mode-config"][1]["address-pool"] = "*l1" },
() => { catalogs["/ip/ipsec/peer"][1].certificate = "ipsec-server" },
() => { catalogs["/ip/ipsec/peer"][1].certificate = "*c2" },
() => { catalogs["/certificate"][4].ca = "*c1" },
() => { catalogs["/ip/ipsec/peer"].push({ ".id": "*p3", name: "owned-peer" }) },
() => { catalogs["/ip/pool"].push({ ".id": "*l1", name: "duplicate-id" }) },
]
for (const change of denied) { reset(); change(); before = commands.length; await assert.rejects(prepare(true)); assert.equal(commands.length, before); const response = await rest.inject({ method: "DELETE", url: `/api/ipsec/server/${serverId}` }); assert.ok([409, 502].includes(response.statusCode), response.body); assert.equal(commands.length, before) }
for(const path of ipsecExternalPoolDependencyPaths){reset();const stalePool=await prepare(true);await approvePlan(stalePool.planId,actor,true);catalogs[path].push({".id":"*consumer",opaque:"NEVER_LATE_POOL_CONSUMER"});before=commands.length;assert.notEqual((await wait((await applyPlan(principal,stalePool.planId,randomUUID())).id)).state,"applied");assert.equal(commands.length,before)}
for (const fault of ["ignored", "lost", "collateral"]) { reset(); mode = fault; before = commands.length; result = await apply(true); assert.ok(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, before + 1); await noReplay(result) }
for (const fault of ["cascade-root-only", "cascade-extra"]) { reset(true); mode = fault; before = commands.length; result = await apply(true); assert.equal(result.state, "partial"); assert.equal(commands.length, before + 8); await noReplay(result) }
reset(); before = commands.length; afterAck = async () => { catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_LATE_OPAQUE" }; result = await apply(true); assert.ok(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, before + 1); await noReplay(result)
reset(); const stale = await prepare(true); await approvePlan(stale.planId, actor, true); catalogs["/ip/ipsec/proposal"][0].opaque = "NEVER_STALE_PROPOSAL"; before = commands.length; assert.notEqual((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, before)
reset(true); const protection = randomUUID(); await originalQuery("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "certificate", id: "*c3", serverId, protection: "deny" }), actor.id])
try { before = commands.length; await assert.rejects(prepare(true)); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/server/${serverId}` })).statusCode, 409); assert.equal(commands.length, before) } finally { await originalQuery("DELETE FROM mcp_protections WHERE id=$1", [protection]) }
reset(); onRead = () => originalQuery("UPDATE servers SET password='changed-source' WHERE id=$1", [serverId]); before = commands.length; await assert.rejects(prepare(true)); assert.equal(commands.length, before); await originalQuery("UPDATE servers SET password='NEVER_ENDPOINT' WHERE id=$1", [serverId])
const readBefore = reads; await setConnectionStatus(access.connection.id, "paused", actor); await assert.rejects(prepare(true)); assert.equal(reads, readBefore)
for (const table of ["mcp_audit", "mcp_plans", "mcp_operation_steps"]) {
const rows = table === "mcp_operation_steps" ? await originalQuery("SELECT s.* FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1", [connectionId]) : await originalQuery(`SELECT * FROM ${table} WHERE connection_id=$1`, [connectionId]); assert.ok(!JSON.stringify(rows.rows).includes("NEVER_"), `${table} leaked private fixture data`)
}
console.log("✓ IPsec server removal SQL/loopback: full managed manifest, REST parity/defaults/noop history, raw foreign/profile/proposal preservation, closed CRL CA cascade, foreign/protected dependencies and ambiguity denied, per-step drift/fault stop without replay; native cascade/forwarding gate remains open")
} finally {
closeFixtureAgent()
pool.query = originalQuery as typeof pool.query
if (connectionId) await setConnectionStatus(connectionId, "paused", actor)
await rest.close(); if (inserted) await originalQuery("DELETE FROM servers WHERE id=$1", [serverId]); router.closeAllConnections(); await new Promise<void>(resolve => router.close(() => resolve()))
}
}