diff --git a/backend/src/modules/mcp/gre-ikev2-pair-actions.test.ts b/backend/src/modules/mcp/gre-ikev2-pair-actions.test.ts index 343f4a55..9bc647be 100644 --- a/backend/src/modules/mcp/gre-ikev2-pair-actions.test.ts +++ b/backend/src/modules/mcp/gre-ikev2-pair-actions.test.ts @@ -1,7 +1,7 @@ import assert from "node:assert/strict" import test from "node:test" import { greIpsecPaths } from "../../services/gre-ipsec-transport.js" -import { greIke2DefaultProposal, greIke2SideCommands, greIke2StableCatalogues, planGreIke2Side, validateGreIke2Defaults, verifyGreIke2Create, verifyGreIke2Update } from "./gre-ikev2-pair-actions.js" +import { greIke2DefaultProposal, greIke2SideCommands, greIke2StableCatalogues, greIke2StepInput, greIke2StepSchema, planGreIke2Side, validateGreIke2Defaults, verifyGreIke2Create, verifyGreIke2Update } from "./gre-ikev2-pair-actions.js" import { redact } from "./security.js" const pair = { endpoints: [ @@ -74,6 +74,19 @@ test("resumes a partial side and changes only the verified old test policy's pro assert.equal(verifyGreIke2Update(catalogs, after, patch), false) }) +test("the runner can parse the prepared plan after its JSON round trip", () => { + const planned = greIke2StepInput(pair, 0, secret, "source-proof", [ + { path: greIpsecPaths.peer, fields: {} }, + { path: greIpsecPaths.policy, id: "*4", fields: { proposal: "default" } }, + ]) + assert.deepEqual(greIke2StepSchema.parse(JSON.parse(JSON.stringify(planned))), planned) + assert.deepEqual(planned.writes, [ + { path: greIpsecPaths.peer, action: "create" }, + { path: greIpsecPaths.policy, id: "*4", action: "update" }, + ]) + assert.throws(() => greIke2StepSchema.parse({ ...planned, writes: [{ path: greIpsecPaths.proposal, id: "*4", action: "update" }] })) +}) + test("requires the new plaintext drop to precede existing rules and preserve every prior object", () => { const [command] = greIke2SideCommands(pair, 0, secret), before = empty() before[greIpsecPaths.filter] = [{ ".id": "*A", chain: "input", action: "accept", packets: "19" }] diff --git a/backend/src/modules/mcp/gre-ikev2-pair-actions.ts b/backend/src/modules/mcp/gre-ikev2-pair-actions.ts index 2b4d9665..c4ae87b9 100644 --- a/backend/src/modules/mcp/gre-ikev2-pair-actions.ts +++ b/backend/src/modules/mcp/gre-ikev2-pair-actions.ts @@ -22,12 +22,25 @@ const endpoint = z.object({ outerAddress: z.string().refine(value => isIP(value) === 4 && value !== "0.0.0.0", "Требуется фиксированный внешний IPv4"), }).strict() const schema = z.object({ endpoints: z.array(endpoint).length(2) }).strict() -const stepSchema = schema.extend({ side: z.union([z.literal(0), z.literal(1)]), ipsecSecret: z.string().regex(/^[a-f0-9]{64}$/), beforeFingerprint: z.string() }) +const writePath = z.enum([greIpsecPaths.filter, greIpsecPaths.profile, greIpsecPaths.peer, greIpsecPaths.identity, greIpsecPaths.policy]) +export const greIke2StepSchema = schema.extend({ + side: z.union([z.literal(0), z.literal(1)]), + ipsecSecret: z.string().regex(/^[a-f0-9]{64}$/), + beforeFingerprint: z.string(), + writes: z.array(z.discriminatedUnion("action", [ + z.object({ path: writePath, action: z.literal("create") }).strict(), + z.object({ path: z.literal(greIpsecPaths.policy), action: z.literal("update"), id: z.string().regex(/^\*[a-z0-9]+$/i) }).strict(), + ])).max(6), +}) type Pair = z.infer type Side = 0 | 1 type Fields = Record type Catalogue = Record type RouterWrite = { path: string; fields: Fields; id?: string } +export function greIke2StepInput(input: Pair, side: Side, ipsecSecret: string, beforeFingerprint: string, commands: RouterWrite[]) { + const writes = commands.map(({ path, id }) => id ? { path, id, action: "update" } : { path, action: "create" }) + return greIke2StepSchema.parse({ endpoints: input.endpoints, side, ipsecSecret, beforeFingerprint, writes }) +} const sides = [0, 1] as const const paths = Object.values(greIpsecPaths) const runtimeFields: Readonly>> = { @@ -193,14 +206,16 @@ async function readSide(input: Pair, side: Side) { export function registerGreIke2PairActions(register: typeof add) { registerMcpService(operation, { current: async raw => { - const input = stepSchema.parse(raw) + const input = greIke2StepSchema.parse(raw) return await Promise.all(sides.map(async side => (await readSide(input, side)).proof)) }, execute: async raw => { - const input = stepSchema.parse(raw), side = input.side, state = await readSide(input, side) + const input = greIke2StepSchema.parse(raw), side = input.side, state = await readSide(input, side) if (state.proof !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "GRE или IPsec изменился после подготовки", 409) const desired = greIke2SideCommands(input, side, input.ipsecSecret) const commands = planGreIke2Side(input, side, state.catalogs, desired) + const reviewed = greIke2StepInput(input, side, input.ipsecSecret, state.proof, commands).writes + if (fingerprint(reviewed) !== fingerprint(input.writes)) throw new McpError("STATE_CONFLICT", "Подготовленные команды IKEv2 отличаются от плана", 409) let before = greIke2StableCatalogues(state.catalogs), confirmed = 0, verified = 0 for (const command of commands) { let dispatched = false @@ -234,14 +249,14 @@ export function registerGreIke2PairActions(register: typeof add) { return { verified: true, commandCount: commands.length, afterFingerprint, greEnabled: false, forwardingVerified: false } }, verify: async (raw, result) => { - const input = stepSchema.parse(raw) + const input = greIke2StepSchema.parse(raw) const receipt = result as { verified?: boolean; afterFingerprint?: string } | null return receipt?.verified === true && receipt.afterFingerprint === completedPairProof(input, input.side, (await readSide(input, input.side)).catalogs, greIke2SideCommands(input, input.side, input.ipsecSecret)) }, }) register(operation, "mm_gre_ikev2_pair_configure_plan", "План настройки отдельного IKEv2 для двух существующих GRE", "network", "mm:network:write", "write", "servers", schema, { - version: 2, + version: 3, target: raw => target(schema.parse(raw)), prepare: async raw => { const input = schema.parse(raw), scope = target(input) @@ -250,10 +265,9 @@ export function registerGreIke2PairActions(register: typeof add) { validateGreIke2Defaults(greIke2DefaultProposal(catalogs[0]), greIke2DefaultProposal(catalogs[1])) const ipsecSecret = pairSecret(input, catalogs) const changes = sides.map(side => planGreIke2Side(input, side, catalogs[side], greIke2SideCommands(input, side, ipsecSecret))) - const steps = sides.map(side => { - const writes = changes[side].map(command => ({ path: command.path, id: command.id, action: command.id ? "update" : "create" })) - return { kind: "service" as const, service: operation, input: { ...input, side, ipsecSecret, beforeFingerprint: before[side].proof, writes } } - }) + const steps = sides.map(side => ({ kind: "service" as const, service: operation, + input: greIke2StepInput(input, side, ipsecSecret, before[side].proof, changes[side]), + })) const objects = (await Promise.all(before.map(state => greMutationProtectionObjects(Number(state.server.id), state.gre)))).flat() return { target: scope, objects, snapshots: [], before: before.map(state => state.proof), steps, mutationCost: Math.max(1, changes.flat().length), requiresHuman: true, warnings: [