Files
MikrotikManager/backend/src/services/ipsec-revision-restore-model.test.ts
T

82 lines
9.8 KiB
TypeScript

import assert from "node:assert/strict"
import { canonicalIpsecSnapshot } from "./entity-snapshots.js"
import { ipsecPeerDeletePaths } from "./ipsec-peer-delete.js"
import { ipsecExternalPoolDependencyPaths } from "./ipsec-pool-dependencies.js"
import { ipsecManagedComment } from "./ipsec-config.js"
import { planIpsecRevisionRestore, projectIpsecRevisionRestore, type IpsecRevisionRestoreState } from "./ipsec-revision-restore-model.js"
import { ipsecServerInitValue } from "./ipsec-server-init.js"
const comment = ipsecManagedComment("restore")
const base = (): IpsecRevisionRestoreState => ({ source: "source", proof: "unused", protections: [], catalogs: {
...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: "existing-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": "owned-pool", responder: "true", "static-dns": "192.0.2.53", opaque: "NEVER_MC" }],
"/ip/pool": [{ ".id": "*5", name: "owned-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: "existing-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: "existing-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: "owned-pool", staticDns: "" }], pools: [{ name: "owned-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 }],
})
const state = base(), before = structuredClone(state), plan = planIpsecRevisionRestore(state, desired)
assert.deepEqual(state, before); assert.equal(plan.commands.length, 7)
const identity = plan.commands.find(command => command.path === "/ip/ipsec/identity" && command.id === "*2")!
assert(!Object.hasOwn(identity.fields, "secret")); assert.equal(identity.fields["remote-id"], "")
const nat = plan.commands.find(command => command.path === "/ip/firewall/nat")!
const mcCommand = plan.commands.find(command => command.path === "/ip/ipsec/mode-config")!
assert(!Object.hasOwn(mcCommand.fields, "responder")); assert(!Object.hasOwn(mcCommand.fields, "address-prefix-length"))
assert.equal(nat.fields["out-interface-list"], ""); assert.equal(nat.fields["ipsec-policy"], "")
assert(!plan.commands.some(command => /certificate|profile|proposal|group/.test(command.path)))
let projected = state
for (const command of plan.commands) projected = projectIpsecRevisionRestore(projected, command)
assert.equal(projected.catalogs["/ip/ipsec/identity"][0].secret, "NEVER_PSK")
assert.equal(projected.catalogs["/ip/ipsec/policy"][0]["dst-address"], "10.78.0.0/24")
assert.equal(projected.catalogs["/ip/ipsec/policy"][0].opaque, "NEVER_POLICY")
assert.deepEqual(projected.catalogs["/certificate"], state.catalogs["/certificate"])
const unusualMc = base(); unusualMc.catalogs["/ip/ipsec/mode-config"][0].responder = "false"; unusualMc.catalogs["/ip/ipsec/mode-config"][0]["address-prefix-length"] = "24"
let unusualAfter = unusualMc; for (const command of planIpsecRevisionRestore(unusualMc, desired).commands) unusualAfter = projectIpsecRevisionRestore(unusualAfter, command)
assert.equal(unusualAfter.catalogs["/ip/ipsec/mode-config"][0].responder, "false"); assert.equal(unusualAfter.catalogs["/ip/ipsec/mode-config"][0]["address-prefix-length"], "24")
const omitted = projectIpsecRevisionRestore(state, nat, { observedRow: { ".id": "*7" } })
assert(!Object.hasOwn(omitted.catalogs["/ip/firewall/nat"][0], "out-interface-list")); assert.equal(omitted.catalogs["/ip/firewall/nat"][0].opaque, "NEVER_NAT")
for (const mutate of [
(s: IpsecRevisionRestoreState) => { s.catalogs["/certificate"] = [] },
(s: IpsecRevisionRestoreState) => { s.catalogs["/certificate"][1]["private-key"] = "false" },
(s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/profile"] = [] },
(s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/proposal"] = [] },
(s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/policy/group"] = [] },
(s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/policy"] = [] },
(s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/mode-config"] = [] },
(s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/identity"][0]["auth-method"] = "digital-signature" },
(s: IpsecRevisionRestoreState) => { delete s.catalogs["/ip/ipsec/identity"][0].secret },
(s: IpsecRevisionRestoreState) => { s.catalogs["/ip/pool"][0].dynamic = "true" },
(s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/policy"].push({ ".id": "*foreign", peer: "other, *0001", comment: "foreign" }) },
]) { const bad = base(); mutate(bad); assert.throws(() => planIpsecRevisionRestore(bad, desired)) }
for (const path of ipsecExternalPoolDependencyPaths) {
const bad = base(); bad.catalogs[path].push({ ".id": "*allocator", [path.startsWith("/ppp/") ? "remote-address" : "address-pool"]: "*0005" }); assert.throws(() => planIpsecRevisionRestore(bad, desired))
}
for (const change of [{ peers: [{ ...desired.peers[0], certificate: "server-cert" }] }, { modeConfigs: [{ ...desired.modeConfigs[0], comment }] }, { policies: [...desired.policies, { ...desired.policies[0], comment: ipsecManagedComment("missing") }] }, { identities: [...desired.identities, { ...desired.identities.find(row => row.authMethod === "pre-shared-key")!, comment: ipsecManagedComment("new-hidden") }] }]) assert.throws(() => planIpsecRevisionRestore(state, { ...desired, ...change }))
const create = { ...desired, pools: [...desired.pools, { name: "new-pool", ranges: "192.0.2.2-192.0.2.10", comment: ipsecManagedComment("new") }] }
const creation = planIpsecRevisionRestore(state, canonicalIpsecSnapshot(create)).commands.find(command => command.action === "create")!
assert.throws(() => projectIpsecRevisionRestore(state, creation, { observedRow: { ".id": "*new", ...creation.fields } }))
const created = projectIpsecRevisionRestore(state, creation, { assignedId: "*new", observedRow: { ".id": "*new", ...creation.fields, "opaque-default": "allowed-only-new" } })
assert.equal(created.catalogs["/ip/pool"].at(-1)!["opaque-default"], "allowed-only-new")
const identityCreatePlan = planIpsecRevisionRestore(state, canonicalIpsecSnapshot({ ...desired, identities: [...desired.identities, { ...desired.identities.find(row => row.authMethod === "pre-shared-key")!, secret: "NEVER_NEW_PSK", comment: ipsecManagedComment("new-explicit") }] }))
const identityCreate = identityCreatePlan.commands.find(command => command.action === "create" && command.path === "/ip/ipsec/identity")!
assert.equal(identityCreate.expectedFields!["remote-id"], ""); assert(!Object.hasOwn(identityCreate.fields, "remote-id"))
assert.throws(() => projectIpsecRevisionRestore(state, identityCreate, { assignedId: "*new", observedRow: { ".id": "*new", ...identityCreate.fields, "remote-id": "auto" } }))
projectIpsecRevisionRestore(state, identityCreate, { assignedId: "*new", observedRow: { ".id": "*new", ...identityCreate.fields } })
assert.throws(() => planIpsecRevisionRestore(state, { ...desired, peers: [...desired.peers, { ...desired.peers[0], name: "new-peer", profile: "" }] }))
const drift = structuredClone(state); drift.catalogs["/ip/ipsec/profile"][0].opaque = "changed"; assert.notDeepEqual(ipsecServerInitValue(drift), ipsecServerInitValue(state))
assert.throws(() => planIpsecRevisionRestore(state, {}))
assert.throws(() => planIpsecRevisionRestore(state, { ...desired, identities: [{ ...desired.identities[0], authMethod: "unknown-secret-mode", comment: ipsecManagedComment("unknown"), secret: "(hidden)" }] }))
console.log("IPsec revision model: complete recorded fields/policies, exact clears, hidden PSK preservation, ACK defaults, PKI/profile/group/proposal/NAT/pool/foreign/system preflight and explicit unsupported policy creation")