Files
MikrotikManager/backend/src/services/wireguard-import-plan.test.ts
T

45 lines
4.8 KiB
TypeScript

import assert from "node:assert/strict"
import { parseWireguardImportPlan, wireguardImportPreview, wireguardImportStages, wireguardImportObjects, WIREGUARD_IMPORT_MAX_BYTES } from "./wireguard-import-plan.js"
import { mcpPolicyPreviewRequestSchema } from "@mmapp/contracts/mcp"
import { evaluateMcpPolicy } from "../modules/mcp/policy.js"
import { sameWireguardEndpoint } from "./wireguard-source.js"
import type { ServerRow } from "../modules/servers/repository/servers-repository.js"
const key = (byte: number) => Buffer.alloc(32, byte).toString("base64")
const content = `[Interface]\nName = wg-import\nPrivateKey = ${key(11)}\nAddress = 2001:db8::1/64\nListenPort = 54321\nMTU = 1300\n[Peer]\nPublicKey = ${key(12)}\nAllowedIPs = 192.0.2.0/24,2001:db8:1::/64\nEndpoint = [2001:db8::2]:13231\nPersistentKeepalive = 25\n[Peer]\nPublicKey = ${key(13)}\nAllowedIPs = 198.51.100.3/32\n`
const plan = parseWireguardImportPlan(content, "auto", 141)
assert.equal(plan.peers.length, 2)
assert.equal(plan.interface.address, "2001:db8::1/64")
assert.equal(plan.peers[0].endpointAddress, "2001:db8::2")
assert.equal(plan.peers[0].endpointPort, 13231)
assert.equal(plan.interface.privateKey, key(11))
assert.equal(wireguardImportStages(plan).length, 8)
const preview = JSON.stringify(wireguardImportPreview(plan))
for (const secret of [key(11), key(12), key(13)]) assert.equal(preview.includes(secret), false)
plan.interface.comment = key(11)
plan.peers[0].comment = `${key(12)} ${key(13)}`
for (const secret of [key(11), key(12), key(13)]) assert.equal(JSON.stringify(wireguardImportPreview(plan)).includes(secret), false, "Even imported names/comments cannot echo keys")
for (const bad of [content.replace(key(13), key(12)), content + "\n[Peer]\nPublicKey=bad\nAllowedIPs=0.0.0.0/0", "x".repeat(WIREGUARD_IMPORT_MAX_BYTES + 1)]) assert.throws(() => parseWireguardImportPlan(bad, "auto", 141))
const rsc = `/interface wireguard add name=wg-rsc private-key="${key(11)}" disabled=yes\n/ipv6 address add interface=wg-rsc address=2001:db8::1/64 advertise=no\n/interface wireguard peers add interface=wg-rsc public-key="${key(12)}" allowed-address=192.0.2.0/24 private-key=none name=first client-address=10.0.0.2 client-dns=1.1.1.1 client-endpoint=vpn.example.org disabled=yes\n/interface wireguard peers add interface=wg-rsc public-key="${key(13)}" allowed-address=2001:db8:1::/64 private-key=auto persistent-keepalive=30 comment=second`
const native = parseWireguardImportPlan(rsc, "rsc", 142)
assert.equal(native.peers.length, 2)
assert.equal(native.peers[0].disabled, true)
assert.equal(native.peers[0].clientDns, "1.1.1.1")
assert.equal(native.peers[0].privateKey, "none")
assert.equal(native.peers[1].privateKey, "auto")
assert.deepEqual(wireguardImportStages(native).map(stage => stage.kind), ["interface", "address", "peer", "peer", "peer-enable", "address-enable"])
const objects = wireguardImportObjects(native, "d31deace-6412-4b5f-bd87-2d3af9ea51f9")
assert.equal(objects.length, 6)
assert.equal(new Set(objects.map(object => object.kind + ":" + object.id)).size, 6)
assert.equal(objects.filter(object => object.kind === "wireguard_peer").length, 2)
for (const secret of [key(11), key(12), key(13)]) assert.equal(JSON.stringify(objects).includes(secret), false)
const now = new Date(), lowBudget = mcpPolicyPreviewRequestSchema.parse({ operationId: "routing.ospf.optimize", policy: { id: "import-budget", version: 1, status: "active", expiresAt: new Date(now.getTime() + 60000).toISOString(), mode: "approval", permissions: ["mm:network:write", "mm:servers:read"], scope: { serverIds: [142], operationIds: ["routing.ospf.optimize"] }, limits: { objectsPerPlan: 2 } }, ownerPermissions: ["mm:network:write", "mm:servers:read"], target: { scope: "servers", serverIds: [142] }, objects, objectCount: 1 })
assert.ok(evaluateMcpPolicy(lowBudget, now).reasons.some(reason => reason.code === "PLAN_LIMIT_EXCEEDED"), "Logical object count independently enforces the object budget")
const endpoint = { id: 142, enabled: true, host: "127.0.0.1", port: 8000, username: "fixture", password: "fixture", useSsl: false, verifySsl: false, routingEndpointEpoch: "d31deace-6412-4b5f-bd87-2d3af9ea51f9" } as ServerRow
assert.equal(sameWireguardEndpoint({ ...endpoint, name: "metadata-only", updatedAt: "later" }, endpoint), true)
assert.equal(sameWireguardEndpoint({ ...endpoint, routingEndpointEpoch: "fa217219-bc44-4194-b776-dda1ff032ffd" }, endpoint), false)
const multiline = parseWireguardImportPlan(rsc.replace("disabled=yes\n", 'disabled=yes comment="first\\nsecond\\tthird"\n').replace("comment=second", 'comment="peer\\r\\ncomment"'), "rsc", 142)
assert.equal(multiline.interface.comment, "first\nsecond\tthird")
assert.equal(multiline.peers[1].comment, "peer\r\ncomment")
console.log("WireGuard import plan: CONF/IPv6 and RSC multiple peers, all peer fields, mixed disabled activation, bounded validation and key-free preview")