diff --git a/backend/src/services/gre-mutation-state.ts b/backend/src/services/gre-mutation-state.ts index fc11bfdf..0bdb0aa7 100644 --- a/backend/src/services/gre-mutation-state.ts +++ b/backend/src/services/gre-mutation-state.ts @@ -117,9 +117,20 @@ export async function readGreMutationState(server: ServerRow, input: GreMutation try{const value=bounded(raw);await check();return value}catch(error){throw stop(error)} } } + // Create and compound edit prove preservation of configured routes. Learned routes + // remain necessary for delete, rename and restore dependency checks. + const configuredRoutesOnly = input?.operation === "create" || (input?.operation === "update" && !options.identityOnly) const [greRaw, ipv4Raw, ipv6Raw, dependencies, bindings, filters] = await settledGreReads([ client.get("/interface/gre").then(value=>readGreNativeRows(rows(value,[".id","name"],100))),client.get("/ip/address").then(value=>rows(value,[".id","address","interface"])),client.get("/ipv6/address").then(value=>rows(value,[".id","address","interface"])), - settledGreReads(greDependencyPaths.map(async path => [path, rows(await client.get(path), path === "/interface" ? [".id", "name"] : [".id"])] as const),stop), + settledGreReads(greDependencyPaths.map(async path => { + const configuredRoute = configuredRoutesOnly && (path === "/ip/route" || path === "/ipv6/route") + const requestPath = configuredRoute ? path + "?static=true" : path + const catalogue = rows(await client.get(requestPath), path === "/interface" ? [".id", "name"] : [".id"]) + if (configuredRoute && catalogue.some(row => observedBoolean(row.dynamic) === true || observedBoolean(row.static) === false)) { + throw greMutationProblem("RouterOS вернул маршруты вне запрошенного фильтра GRE") + } + return [path, catalogue] as const + }),stop), pool.query('SELECT id,interface_name AS "interfaceName" FROM user_interface_bindings WHERE server_id=$1 ORDER BY id LIMIT 1001', [server.id]), pool.query('SELECT id,gateway_tunnel_id AS "gatewayTunnelId",gateway FROM filter_rules WHERE server_id=$1 ORDER BY id LIMIT 1001', [server.id]), ]as const,stop) diff --git a/backend/src/services/gre-mutation.test.ts b/backend/src/services/gre-mutation.test.ts index e60f7478..7ac9acb1 100644 --- a/backend/src/services/gre-mutation.test.ts +++ b/backend/src/services/gre-mutation.test.ts @@ -1,7 +1,9 @@ import assert from "node:assert/strict" import { greMutationSchema } from "./gre-mutation-input.js" import { planGreMutation, verifyGreMutationOp, executeGreMutationOps } from "./gre-mutation.js" -import { assertGreDeleteDependencies, greDependencyPaths, greMutationProof, readGrePskValue, type GreMutationState } from "./gre-mutation-state.js" +import { assertGreDeleteDependencies, greDependencyPaths, greMutationProof, readGreMutationState, readGrePskValue, type GreMutationState } from "./gre-mutation-state.js" +import { pool } from "../db/index.js" +import { MikrotikClient } from "./mikrotik.js" import type { ServerRow } from "../modules/servers/repository/servers-repository.js" import type { RosWriteOp } from "./entity-snapshots.js" @@ -95,4 +97,37 @@ assert.equal(commands,1); assert.equal(state.gre[0].disabled,"true") const deletion = greMutationSchema.parse({operation:"delete",name:"own-gre"}) const deleteOps = planGreMutation(server,deletion,state) assert.equal(deleteOps.at(-1)?.path,"/interface/gre/*C") + +const originalClient = MikrotikClient.fromServer, originalQuery = pool.query +const routeRequests: string[] = [] +const configured = { ".id": "*S", static: "true", dynamic: "false" } +const learned = Array.from({ length: 25_626 }, (_, i) => ({ ".id": `*${(i + 100).toString(16)}`, dynamic: "true", bgp: "true" })) +let ignoresFilter = false +MikrotikClient.fromServer = (() => ({ + async get(path: string) { + if (!/^\/(?:ip|ipv6)\/route(?:\?|$)/.test(path)) return [] + routeRequests.push(path) + if (path.endsWith("?static=true")) return ignoresFilter ? [configured, learned[0]] : [configured] + return [configured, ...learned] + }, + async post() { throw new Error("Unexpected native read in route catalogue test") }, +})) as unknown as typeof MikrotikClient.fromServer +pool.query = (async () => ({ rows: [] })) as unknown as typeof pool.query +try { + const configuredPaths = ["/ip/route?static=true", "/ipv6/route?static=true"] + for (const input of [create, greMutationSchema.parse({ operation: "update", name: "test-gre", fields: { enabled: true } })]) { + routeRequests.length = 0 + const snapshot = await readGreMutationState(server, input) + assert.deepEqual(routeRequests.sort(), configuredPaths.sort()) + assert.deepEqual(snapshot.dependencies["/ip/route"], [configured]) + } + ignoresFilter = true + await assert.rejects(readGreMutationState(server, create), /вне запрошенного фильтра GRE/) + routeRequests.length = 0 + await assert.rejects(readGreMutationState(server, deletion), /каталог зависимостей GRE/) + assert.deepEqual(routeRequests.sort(), ["/ip/route", "/ipv6/route"].sort()) +} finally { + MikrotikClient.fromServer = originalClient + pool.query = originalQuery +} console.log("GRE mutations: disabled staging, v4→v6 primary replacement, opaque/timer/secret preservation, native identity, parent/application dependencies, link-local exclusion, per-command proof and ignored/lost stop")