diff --git a/backend/src/modules/mcp/acceptance-shards.ts b/backend/src/modules/mcp/acceptance-shards.ts index a9ce645f..8605fdf7 100644 --- a/backend/src/modules/mcp/acceptance-shards.ts +++ b/backend/src/modules/mcp/acceptance-shards.ts @@ -12,6 +12,7 @@ export const ACCEPTANCE_SHARDS: Record Promise (await import("./routing-community-context-acceptance.js")).acceptRouteCommunityContextMcp(actor), acceptWireguardPeerCreation: async actor => (await import("./wireguard-peer-create-acceptance.js")).acceptWireguardPeerCreation(actor), acceptWireguardInterfaceUpdate: async actor => (await import("./wireguard-interface-update-acceptance.js")).acceptWireguardInterfaceUpdate(actor), + acceptInterfaceRename: async actor => (await import("./interface-rename-acceptance.js")).acceptInterfaceRename(actor), acceptWireguardImport: async actor => (await import("./wireguard-import-acceptance.js")).acceptWireguardImport(actor), verifyMcpFlowOverlay: async actor => (await import("../../test/mcp-flow-overlay.js")).verifyMcpFlowOverlay(actor), acceptWireguardInterfaceDeletion: async actor => (await import("./wireguard-interface-delete-acceptance.js")).acceptWireguardInterfaceDeletion(actor), diff --git a/backend/src/modules/mcp/actions.ts b/backend/src/modules/mcp/actions.ts index dd712763..8f4e21f5 100644 --- a/backend/src/modules/mcp/actions.ts +++ b/backend/src/modules/mcp/actions.ts @@ -91,6 +91,7 @@ import { registerTerminalDiagnosticActions } from "./terminal-diagnostic-actions import { registerEventsBatchActions } from "./events-batch-actions.js" import { registerGlobalReconciliationActions } from "./global-reconciliation-actions.js" import { registerWireguardInterfaceUpdateActions } from "./wireguard-interface-update-actions.js" +import { registerInterfaceRenameActions } from "./interface-rename-actions.js" import { registerWireguardImportActions } from "./wireguard-import-actions.js" import { registerWireguardExportActions } from "./wireguard-export-actions.js" import { registerServerConnectionActions } from "./server-connection-actions.js" @@ -710,6 +711,7 @@ registerTerminalDiagnosticActions(add) registerEventsBatchActions(add) registerGlobalReconciliationActions(add) registerWireguardInterfaceUpdateActions(add) +registerInterfaceRenameActions(add) registerWireguardImportActions(add) registerServerConnectionActions(add) registerIpsecPeerDeleteActions(add) diff --git a/backend/src/modules/mcp/integration.test.ts b/backend/src/modules/mcp/integration.test.ts index e515ce68..ceeea6dc 100644 --- a/backend/src/modules/mcp/integration.test.ts +++ b/backend/src/modules/mcp/integration.test.ts @@ -3028,6 +3028,7 @@ try { await soft("verifyMcpTerminalDiagnostics", async () => (await import("../../test/mcp-terminal-diagnostics.js")).verifyMcpTerminalDiagnostics(actor, app)) await soft("verifyMcpEventsBatch", async () => (await import("../../test/mcp-events-batch.js")).verifyMcpEventsBatch(actor, app)) await shardable("acceptWireguardInterfaceUpdate") + await shardable("acceptInterfaceRename") await shardable("acceptWireguardImport") await soft("acceptWireguardExportMcp", async () => (await import("./wireguard-export-acceptance.js")).acceptWireguardExportMcp(actor, app)) await soft("acceptProtectionRegistrationScope", async () => (await import("./protection-scope-acceptance.js")).acceptProtectionRegistrationScope(actor)) diff --git a/backend/src/modules/mcp/interface-rename-acceptance.ts b/backend/src/modules/mcp/interface-rename-acceptance.ts new file mode 100644 index 00000000..f7bc5bab --- /dev/null +++ b/backend/src/modules/mcp/interface-rename-acceptance.ts @@ -0,0 +1,110 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { approvePlan, applyPlan, invokeMcpTool } from "./runtime.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "./store.js" + +export async function acceptInterfaceRename(actor: AuthUser) { + const serverId = 174 + const initial: Record[]> = { + "/interface": [ + { ".id": "*7", name: "wg1", type: "wg", dynamic: "false" }, + { ".id": "*8", name: "ether1", "default-name": "ether1", type: "ether" }, + { ".id": "*9", name: "radio one", type: "wifi" }, + { ".id": "*a", name: "dynamic1", type: "ppp", dynamic: "true" }, + { ".id": "*b", name: "wg1-backup", type: "wg" }, + ], + "/ip/address": [{ ".id": "*15", interface: "wg1", "actual-interface": "wg1", address: "192.0.2.1/30" }], + "/ipv6/address": [{ ".id": "*16", interface: "*7", address: "2001:db8::1/64" }], + "/interface/bridge/port": [{ ".id": "*14", bridge: "bridge1", interface: "ether1" }], + "/interface/wireguard/peers": [{ ".id": "*11", interface: "wg1", name: "other-site", "public-key": "fixture-public-key", "allowed-address": "192.0.2.2/32", "endpoint-port": "13235" }], + "/ip/firewall/filter": [ + { ".id": "*20", chain: "input", action: "accept", "in-interface": "wg1", protocol: "udp" }, + { ".id": "*21", chain: "forward", action: "drop", "in-interface": "!wg1", "out-interface": "wg1-backup" }, + ], + "/ip/route": [{ ".id": "*22", static: "true", gateway: "192.0.2.2%wg1@main", "dst-address": "198.51.100.0/24" }, ...Array.from({ length: 12_608 }, (_, i) => ({ ".id": `*r${i.toString(16)}`, dynamic: "true", gateway: "192.0.2.2%ether1" }))], + } + let rows = structuredClone(initial), reads = 0, mode: "normal" | "stale" | "lost" = "normal" + const writes: { path: string; fields: Record }[] = [] + const router = createServer(async (req, res) => { + const url = new URL(req.url ?? "/", "http://fixture.invalid"), path = url.pathname.slice(5) + res.setHeader("content-type", "application/json") + if (req.method === "GET") { + let records = rows[path] ?? [] + if (url.searchParams.get("static") === "true") records = records.filter(row => row.static === "true") + const properties = url.searchParams.get(".proplist")?.split(",") + if (properties) records = records.map(row => Object.fromEntries(Object.entries(row).filter(([key]) => properties.includes(key)))) + if (path === "/ip/firewall/filter") records = records.map(row => ({ ...row, packets: String(++reads) })) + res.end(JSON.stringify(records)); return + } + if (req.method !== "PATCH" || !/^\/interface\/\*[a-z0-9]+$/i.test(path)) { res.statusCode = 400; res.end("{}"); return } + let body = ""; for await (const chunk of req) body += chunk + const fields: Record = JSON.parse(body), target = rows["/interface"].find(row => path.endsWith("/" + row[".id"]))! + const old = target.name + writes.push({ path, fields }) + Object.assign(target, fields) + if (mode !== "stale") { + for (const rowsForPath of Object.values(rows)) for (const row of rowsForPath) for (const key of ["interface", "actual-interface", "in-interface", "out-interface"]) { + if (row[key] === old) row[key] = target.name + if (row[key] === "!" + old) row[key] = "!" + target.name + } + for (const row of rows["/ip/route"]) if (row.gateway.endsWith(`%${old}@main`)) row.gateway = row.gateway.replace(`%${old}@main`, `%${target.name}@main`) + } + if (mode === "lost") { req.socket.destroy(); return } + res.end(JSON.stringify(target)) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + let connectionId: string | undefined + const protection = randomUUID() + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl) VALUES($1,'interface-rename-fixture','127.0.0.1',$2,false)", [serverId, (router.address() as { port: number }).port]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "interface rename", mode: "approval", expiresAt: new Date(Date.now() + 1_800_000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [serverId], operationIds: ["interfaces.rename"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const plan = async (iface: string, name: string) => (await invokeMcpTool(principal, "mm_interface_rename_plan", { serverId, interface: iface, name })) as { planId: string } + const execute = async (iface: string, name: string) => { + const { planId } = await plan(iface, name) + await approvePlan(planId, actor, true) + const { id } = await applyPlan(principal, planId, randomUUID()) + for (let i = 0; i < 500; i++) { + const result = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (result && !["pending", "running"].includes(result.state)) return result + await delay(20) + } + throw new Error("Interface rename remains live") + } + await assert.rejects(plan("dynamic1", "renamed"), { code: "PROTECTED_OBJECT" }) + await assert.rejects(plan("*7", "ether1"), { code: "STATE_CONFLICT" }) + assert.equal(writes.length, 0) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "ip_filter", id: "*20", serverId, protection: "deny" }), actor.id]) + await assert.rejects(plan("*7", "protected-test"), { code: "PROTECTED_OBJECT" }) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + assert.equal((await execute("wg1", "wg-to-msk-gw01")).state, "applied") + assert.deepEqual(writes[0], { path: "/interface/*7", fields: { name: "wg-to-msk-gw01" } }) + assert.deepEqual(rows["/ip/firewall/filter"].map(row => row["in-interface"]), ["wg-to-msk-gw01", "!wg-to-msk-gw01"]) + assert.equal(rows["/ip/firewall/filter"][1]["out-interface"], "wg1-backup") + assert.equal(rows["/interface/wireguard/peers"][0].interface, "wg-to-msk-gw01") + assert.equal(rows["/ipv6/address"][0].interface, "*7") + assert.equal(rows["/ip/route"][0].gateway, "192.0.2.2%wg-to-msk-gw01@main") + assert.equal((await execute("*8", "ether-wan")).state, "applied") + assert.equal(rows["/interface/bridge/port"][0].interface, "ether-wan") + assert.equal((await execute("radio one", "wifi guest")).state, "applied") + assert.equal(writes.length, 3) + mode = "stale" + assert.equal((await execute("wg-to-msk-gw01", "wg-incomplete")).error_code, "VERIFICATION_FAILED") + assert.equal(writes.length, 4) + rows = structuredClone(initial); mode = "lost" + assert.equal((await execute("wg1", "wg-unknown")).error_code, "MUTATION_OUTCOME_UNKNOWN") + assert.equal(writes.length, 5) + } finally { + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) + router.closeAllConnections() + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/interface-rename-actions.ts b/backend/src/modules/mcp/interface-rename-actions.ts new file mode 100644 index 00000000..18da3407 --- /dev/null +++ b/backend/src/modules/mcp/interface-rename-actions.ts @@ -0,0 +1,186 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { assertVerifiedConfigurationAccess, assertVerifiedConfigurationReadAccess } from "../../services/config-mutation-policy.js" +import { greStableRows, assertGreDeleteDependencies } from "../../services/gre-mutation-state.js" +import { observedBoolean } from "../../services/ospf-observed-values.js" +import { encodeRosId, isRouterOsNativeId } from "../../services/routeros-native-paths.js" +import type { add } from "./actions.js" +import type { ChangeEvidence } from "./action-types.js" +import { rosSnapshotSourceProof, routerClient } from "./resources.js" +import { fingerprint, McpError, privateSecretFingerprint } from "./security.js" +import { registerMcpService } from "./service-registry.js" + +const service = "interfaces.rename" +const name = z.string().min(1).max(100).refine(value => value === value.trim() && !/[\u0000-\u001f\u007f]/.test(value)) +const schema = z.object({ serverId: z.number().int().positive().safe(), interface: name, name }).strict() +const recordSchema = z.object({ serverId: schema.shape.serverId, id: z.string().max(200).refine(isRouterOsNativeId), from: name, to: name }).strict() +type Rename = z.infer +type Row = Record +type Reference = "single" | "list" | "gateway" | "negated" + +const inventory = "/interface?.proplist=.id,name,type,default-name,dynamic,builtin,default,disabled,mtu,l2mtu,comment" +const firewall = (["ip", "ipv6"] as const).flatMap(family => ["filter", "nat", "mangle", "raw"].map(table => ({ + kind: `${family}_${table}`, path: `/${family}/firewall/${table}`, fields: { "in-interface": "negated", "out-interface": "negated" } as const, ordered: true, +}))) +const references: { kind: string; path: string; fields: Record; ordered?: boolean; wireguard?: boolean }[] = [ + { kind: "ipv4_address", path: "/ip/address", fields: { interface: "single", "actual-interface": "single" } }, + { kind: "ipv6_address", path: "/ipv6/address", fields: { interface: "single", "actual-interface": "single" } }, + { kind: "interface_list_member", path: "/interface/list/member", fields: { interface: "single" } }, + { kind: "bridge_port", path: "/interface/bridge/port", fields: { interface: "single", bridge: "single" } }, + { kind: "bridge_vlan", path: "/interface/bridge/vlan", fields: { bridge: "single", tagged: "list", untagged: "list" } }, + { kind: "vlan", path: "/interface/vlan", fields: { interface: "single" } }, + { kind: "bonding", path: "/interface/bonding", fields: { slaves: "list" } }, + { kind: "vrrp", path: "/interface/vrrp", fields: { interface: "single", "group-authority": "single" } }, + { kind: "wireguard_peer", path: "/interface/wireguard/peers?.proplist=.id,interface,name,public-key,allowed-address,endpoint-address,endpoint-port,persistent-keepalive,disabled,dynamic,comment", fields: { interface: "single" }, wireguard: true }, + { kind: "ipv4_route", path: "/ip/route?static=true", fields: { gateway: "gateway" } }, + { kind: "ipv6_route", path: "/ipv6/route?static=true", fields: { gateway: "gateway" } }, + { kind: "routing_rule", path: "/routing/rule", fields: { interface: "single" }, ordered: true }, + { kind: "vrf", path: "/ip/vrf", fields: { interfaces: "list" }, ordered: true }, + { kind: "ospf_template", path: "/routing/ospf/interface-template", fields: { interfaces: "list" }, ordered: true }, + { kind: "bfd_configuration", path: "/routing/bfd/configuration", fields: { interfaces: "list" }, ordered: true }, + ...firewall, +] + +const fail = (code: string, message: string, status = 409) => new McpError(code, message, status) +const unstable = (path: string) => fail("STATE_UNAVAILABLE", `Не подтверждён каталог RouterOS ${path.split("?")[0]}`, 502) +const unsupported = new Set(["all", "none", "dynamic", "slave", "static", "wg-flow", "lo"]) + +async function catalogue(client: Awaited>["client"], path: string): Promise { + const routes = path.endsWith("?static=true") + let raw: unknown + try { raw = await client.get(path, 10_000, undefined, routes ? 8 * 1024 * 1024 : 1_048_576) } + catch (error) { if (error instanceof McpError) throw error; throw unstable(path) } + if (!Array.isArray(raw) || raw.length > (routes ? 50_000 : 10_000) || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || !isRouterOsNativeId(row[".id"]) || Object.values(row).some(value => typeof value !== "string")) || new Set(raw.map(row => row[".id"])).size !== raw.length) throw unstable(path) + return raw as Row[] +} + +/** RouterOS can render the same typed reference as a name or a native ID. */ +function interfaceRenameReference(value: string, mode: Reference, aliases: ReadonlySet, replacement: string): string { + if (aliases.has(value)) return replacement + if (mode === "negated" && value.startsWith("!") && aliases.has(value.slice(1))) return `!${replacement}` + if (mode === "list" || mode === "gateway") return value.split(",").map(part => { + if (aliases.has(part)) return replacement + if (mode === "gateway") { + const match = /^([^%@]+%)?([^%@]+)(@[^%@]+)?$/.exec(part) + if (match && aliases.has(match[2])) return `${match[1] ?? ""}${replacement}${match[3] ?? ""}` + } + return part + }).join(",") + return value +} + +function targetFrom(rows: Row[], identity: string) { + const target = rows.find(row => row.name === identity || row[".id"] === identity) + if (!target) throw fail("NOT_FOUND", "Интерфейс RouterOS не найден", 404) + if (!target.type || !target.name || rows.filter(row => row.name === target.name || row[".id"] === identity || row.name === identity).length !== 1) throw unstable(inventory) + if (["dynamic", "builtin", "default"].some(key => target[key] !== undefined && observedBoolean(target[key]) !== false) || unsupported.has(target.name.toLowerCase()) || target.comment === "MikrotikManager traffic-flow overlay") throw fail("PROTECTED_OBJECT", "Динамический, системный или управляемый NetFlow интерфейс нельзя переименовать этим планом") + return target +} + +async function applicationReferences(move: Rename) { + const names = [move.id, move.from, move.to] + const { rows } = await pool.query<{ bound: boolean }>(`SELECT ( + EXISTS(SELECT 1 FROM user_interface_bindings WHERE server_id=$1 AND interface_name=ANY($2::text[])) OR + EXISTS(SELECT 1 FROM uptime_probes WHERE src_server_id=$1 AND src_interface=ANY($2::text[])) OR + EXISTS(SELECT 1 FROM uptime_speed_probes WHERE (src_server_id=$1 AND src_interface=ANY($2::text[])) OR (dst_server_id=$1 AND dst_interface=ANY($2::text[]))) + ) AS bound`, [move.serverId, names]) + if (rows[0]?.bound !== false) throw fail("DEPENDENCY_CONFLICT", "Имя или ID используется в настройках приложения; требуется согласованное изменение привязки") + const { rows: filters } = await pool.query<{ id: number; gateway: string; gatewayTunnelId: string }>('SELECT id, gateway, gateway_tunnel_id AS "gatewayTunnelId" FROM filter_rules WHERE server_id=$1 LIMIT 1001', [move.serverId]) + if (filters.length > 1000) throw fail("STATE_UNAVAILABLE", "Не подтверждены зависимости фильтров приложения", 502) + const aliases = new Set(names) + if (filters.some(row => aliases.has(row.gatewayTunnelId) || interfaceRenameReference(row.gateway, "gateway", aliases, "\u0000") !== row.gateway)) throw fail("DEPENDENCY_CONFLICT", "Интерфейс используется BGP-фильтром приложения") +} + +async function read(move: Rename, expectedSource: string, authorize: () => Promise) { + await authorize() + const { client, server } = await routerClient(move.serverId) + if (await rosSnapshotSourceProof(server) !== expectedSource) throw fail("STATE_CONFLICT", "Источник RouterOS изменился") + const interfaces = await catalogue(client, inventory), target = targetFrom(interfaces, move.id) + if (![move.from, move.to].includes(target.name) || interfaces.some(row => row[".id"] !== move.id && row.name === move.to)) throw fail("STATE_CONFLICT", "Интерфейс изменился или новое имя уже занято") + const aliases = new Set([move.from, move.to, move.id]), current = new Set([target.name, move.id]) + const marker = `\u0000${move.id}` + const affected: ChangeEvidence["objects"] = [] + const sections: unknown[] = [] + for (const dependency of references) { + if (dependency.wireguard && !["wg", "wireguard"].includes(target.type)) continue + await authorize() + const rows = await catalogue(client, dependency.path) + if (dependency.path.endsWith("?static=true") && rows.some(row => observedBoolean(row.dynamic) === true || observedBoolean(row.static) === false)) throw unstable(dependency.path) + const selected: Row[] = [] + for (const [position, row] of rows.entries()) { + if (observedBoolean(row.dynamic) === true) continue + const projection = { ...row } + let related = false + for (const [field, mode] of Object.entries(dependency.fields)) { + if (row[field] === undefined || interfaceRenameReference(row[field], mode, aliases, marker) === row[field]) continue + if (interfaceRenameReference(row[field], mode, aliases, marker) !== interfaceRenameReference(row[field], mode, current, marker)) throw fail("DEPENDENCY_CONFLICT", `Несогласованная ссылка на интерфейс в ${dependency.path.split("?")[0]}`) + projection[field] = interfaceRenameReference(row[field], mode, current, marker) + related = true + } + if (!related) continue + affected.push({ kind: dependency.kind, id: row[".id"], serverId: move.serverId }) + // These bridge VLAN memberships are derived by RouterOS from the configured ports and PVID. + if (dependency.kind === "bridge_vlan") { delete projection["current-tagged"]; delete projection["current-untagged"] } + if (dependency.ordered) projection.position = String(position) + selected.push(projection) + } + sections.push([dependency.kind, greStableRows(selected, dependency.ordered)]) + } + const policyPath = "/routing/filter/rule?.proplist=.id,rule" + await authorize() + const routingFilters = await catalogue(client, policyPath) + for (const reference of [move.from, move.to]) assertGreDeleteDependencies({ dependencies: { [policyPath]: routingFilters }, bindings: [], filters: [] }, { name: reference, ".id": move.id }, "Имя интерфейса") + await applicationReferences(move) + await authorize() + if (await rosSnapshotSourceProof((await routerClient(move.serverId)).server) !== expectedSource) throw fail("STATE_CONFLICT", "Источник RouterOS изменился во время чтения") + const identities = interfaces.filter(row => row[".id"] === move.id || observedBoolean(row.dynamic) !== true) + const proof = (targetName: string) => privateSecretFingerprint(fingerprint({ interfaces: greStableRows(identities.map(row => row[".id"] === move.id ? { ...row, name: targetName } : row)), sections }), "mcp-interface-rename-v1") + return { target, affected, before: { name: target.name, proof: await proof(target.name) }, afterProof: await proof(move.to) } +} + +export function registerInterfaceRenameActions(register: typeof add) { + const current = async (input: Record) => (await read(recordSchema.parse(input.move), String(input.source), async () => {})).before + registerMcpService(service, { + current, + execute: async input => { + const move = recordSchema.parse(input.move), source = String(input.source) + const { client } = await routerClient(move.serverId) + try { + await client.patch(`/interface/${encodeRosId(move.id)}`, { name: move.to }, 15_000, async () => { + await assertVerifiedConfigurationAccess() + if ((await read(move, source, assertVerifiedConfigurationReadAccess)).before.proof !== input.beforeProof) throw fail("STATE_CONFLICT", "Интерфейс или его ссылки изменились перед переименованием") + await assertVerifiedConfigurationAccess() + }) + } catch (error) { + if (error instanceof McpError) throw error + throw fail("MUTATION_OUTCOME_UNKNOWN", "Исход переименования не подтверждён; повтор запрещён", 503) + } + return { interfaceId: move.id, previousName: move.from, name: move.to } + }, + verify: async input => { + try { return (await current(input)).proof === input.afterProof } + catch (error) { if (error instanceof McpError && error.code === "DEPENDENCY_CONFLICT") return false; throw error } + }, + reconcile: async (input, authorize) => { + const { before } = await read(recordSchema.parse(input.move), String(input.source), authorize) + return { matchesOriginalState: before.proof === input.beforeProof, matchesDesiredState: before.proof === input.afterProof } + }, + }) + register(service, "mm_interface_rename_plan", "Переименовать интерфейс RouterOS по ID или текущему имени и проверить сохранность привязок", "network", "mm:network:write", "write", "servers", schema, { + prepare: async (input, context) => { + const request = schema.parse(input), { client, server } = await routerClient(request.serverId) + const source = await rosSnapshotSourceProof(server) + const target = targetFrom(await catalogue(client, inventory), request.interface) + if (request.name === target.name || unsupported.has(request.name.toLowerCase()) || isRouterOsNativeId(request.name)) throw fail("INVALID_INPUT", "Укажите новое незарезервированное имя интерфейса", 400) + const move = recordSchema.parse({ serverId: request.serverId, id: target[".id"], from: target.name, to: request.name }) + const state = await read(move, source, async () => { context.signal?.throwIfAborted() }) + if (state.before.name !== move.from) throw fail("STATE_CONFLICT", "Имя интерфейса изменилось во время подготовки") + const kind = ({ wg: "wireguard", ether: "ethernet" } as Record)[state.target.type] ?? state.target.type + const objects = [{ kind: "server", id: String(move.serverId), serverId: move.serverId }, ...[...new Set(["interface", kind])].flatMap(kind => [move.id, move.from, move.to].map(id => ({ kind, id, serverId: move.serverId }))), ...state.affected] + return { target: { scope: "servers", serverIds: [move.serverId] }, objects, snapshots: [], before: state.before, requiresHuman: true, verificationScope: "configuration", + steps: [{ kind: "service", service, input: { serverId: move.serverId, move, source, beforeProof: state.before.proof, afterProof: state.afterProof } }], + warnings: ["Выполняется один RouterOS PATCH имени по native ID. Передача трафика может кратко прерваться.", "Проверяются идентичность интерфейса и ссылки в наблюдаемых каталогах RouterOS. Пользовательские скрипты и история метрик по имени не переписываются.", "При неясном исходе требуется сверка. Команда не повторяется автоматически."], + } + }, + }) +} diff --git a/backend/src/modules/mcp/policy.test.ts b/backend/src/modules/mcp/policy.test.ts index 8223e12f..995c0880 100644 --- a/backend/src/modules/mcp/policy.test.ts +++ b/backend/src/modules/mcp/policy.test.ts @@ -55,6 +55,24 @@ test("unattended plans require an explicit service policy and retain server prot denied(input, "PROTECTED_OBJECT") }) +test("service rate exemptions keep plan size, server scope and approval checks", () => { + const input = request() + assert.equal(mcpConnectionPolicySchema.safeParse({ ...input.policy, unlimitedRates: true }).success, false) + input.policy = mcpConnectionPolicySchema.parse({ ...input.policy, authority: "service", expiresAt: MCP_SERVICE_EXPIRATION, unlimitedRates: true }) + input.usage = { callsInMinute: 10_000, concurrentReads: 100, mutationsInHour: 10_000 } + const read = { ...input, operationId: "logs.query" } + assert.equal(evaluateMcpPolicy(read, now).decision, "allowed") + const mutation = mcpPolicyDecisionSchema.parse(evaluateMcpPolicy(input, now)) + assert.equal(mutation.unlimitedRates, true) + assert.equal(mutation.decision, "approval-required") + denied({ ...input, objectCount: input.policy.limits.objectsPerPlan + 1 }, "PLAN_LIMIT_EXCEEDED") + denied({ ...input, target: { scope: "servers", serverIds: [99] } }, "SERVER_DENIED") + denied({ ...input, protections: [{ kind: "server", id: "1", protection: "deny" }] }, "PROTECTED_OBJECT") + delete input.policy.unlimitedRates + denied(input, "BUDGET_EXCEEDED") + denied(read, "RATE_LIMITED") +}) + test("observer reads permitted logs, but cannot run bandwidth or create users", () => { const read = request("logs.query"); read.policy.mode = "observer" assert.equal(evaluateMcpPolicy(read, now).decision, "allowed") diff --git a/backend/src/modules/mcp/policy.ts b/backend/src/modules/mcp/policy.ts index 0923c3e8..779a635a 100644 --- a/backend/src/modules/mcp/policy.ts +++ b/backend/src/modules/mcp/policy.ts @@ -12,6 +12,10 @@ export function hasUnattendedPlanAuthority(policy: McpConnectionPolicy) { return policy.authority === "service" && policy.mode === "autonomous" && policy.allowUnattendedPlans === true } +export function hasUnlimitedMcpRates(policy: McpConnectionPolicy) { + return policy.authority === "service" && policy.unlimitedRates === true +} + export function operationApprovalRequirement(policy: McpConnectionPolicy, operationId: string, target: McpPolicyPreviewRequest["target"], requiresHuman?: boolean): Pick { if (!requiresHuman || hasUnattendedPlanAuthority(policy)) return { objects: [], protections: [] } const objects = target.scope === "servers" ? [] : [{ kind: "mcp-operation", id: operationId }] @@ -24,6 +28,7 @@ export function evaluateMcpPolicy(payload: McpPolicyPreviewRequest, now = new Da const input = mcpPolicyPreviewRequestSchema.parse(payload) if (!Number.isFinite(now.getTime())) throw new Error("Некорректное время проверки MCP") const { policy, target, usage } = input + const unlimitedRates = hasUnlimitedMcpRates(policy) const denied: McpPolicyDecision["reasons"] = [] const approvals: McpPolicyDecision["reasons"] = [] const deny = (code: McpPolicyReasonCode, message: string) => { denied.push({ code, message }) } @@ -32,6 +37,7 @@ export function evaluateMcpPolicy(payload: McpPolicyPreviewRequest, now = new Da decision: denied.length ? "denied" : approvals.length ? "approval-required" : "allowed", reasons: [...denied, ...approvals], limits: policy.limits, + ...(unlimitedRates && { unlimitedRates: true }), }) const operation = findMcpOperation(input.operationId) if (!operation) { @@ -72,15 +78,15 @@ export function evaluateMcpPolicy(payload: McpPolicyPreviewRequest, now = new Da deny("INVALID_OBJECT_SCOPE", "Объект или зависимость находится вне области запроса") } - if (usage.callsInMinute >= policy.limits.callsPerMinute) deny("RATE_LIMITED", "Исчерпан минутный лимит вызовов") + if (!unlimitedRates && usage.callsInMinute >= policy.limits.callsPerMinute) deny("RATE_LIMITED", "Исчерпан минутный лимит вызовов") if (operation.effect === "read") { - if (usage.concurrentReads >= policy.limits.concurrentReads) deny("RATE_LIMITED", "Исчерпан лимит одновременного чтения") + if (!unlimitedRates && usage.concurrentReads >= policy.limits.concurrentReads) deny("RATE_LIMITED", "Исчерпан лимит одновременного чтения") return result() } if (policy.mode === "observer") deny("OBSERVER_WRITE_DENIED", "Наблюдателю запрещены изменения и нагрузочные проверки") if (!input.auditAvailable) deny("AUDIT_UNAVAILABLE", "До начала изменений требуется доступный аудит") - if (usage.mutationsInHour >= policy.limits.mutationsPerHour || usage.mutationsInHour + input.objectCount > policy.limits.mutationsPerHour) deny("BUDGET_EXCEEDED", "Недостаточно часового бюджета для всех изменений плана") + if (!unlimitedRates && (usage.mutationsInHour >= policy.limits.mutationsPerHour || usage.mutationsInHour + input.objectCount > policy.limits.mutationsPerHour)) deny("BUDGET_EXCEEDED", "Недостаточно часового бюджета для всех изменений плана") const affectedServers = new Set([ ...(target.scope === "servers" ? target.serverIds : []), ...input.objects.flatMap(object => object.serverId === undefined ? [] : [object.serverId]), diff --git a/backend/src/modules/mcp/protocol-limits.owned.test.ts b/backend/src/modules/mcp/protocol-limits.owned.test.ts index e99404cf..9222fb45 100644 --- a/backend/src/modules/mcp/protocol-limits.owned.test.ts +++ b/backend/src/modules/mcp/protocol-limits.owned.test.ts @@ -20,12 +20,12 @@ const { pool, closePool } = await import("../../db/index.js") const { SYSTEM_DATABASE_OPERATION_LOCK, SYSTEM_DATABASE_REQUEST_LOCK, withOperationLock, withSystemDatabaseOperationLock } = await import("../../db/operation-lock.js") const { applySqlMigrations, applyStandaloneSqlMigrations } = await import("../../db/migrate.js") const { PARTITION_WRITER_ADVISORY_KEY } = await import("../../services/partition-write-transaction.js") -const { authenticateMcpToken, createMcpConnection, updateMcpSettings } = await import("./store.js") +const { authenticateMcpToken, createMcpConnection, updateMcpConnection, updateMcpSettings } = await import("./store.js") const { recordUnauthorizedProtocol, reserveProtocolEnvelope } = await import("./protocol-limits.js") const { canonicalMcpUrl, encryptSecret, tokenDigest } = await import("./security.js") const { buildApp } = await import("../../index.js") const { sharedDatabaseGate } = await import("../../services/shared-database-gate.js") -const { invokeMcpTool } = await import("./runtime.js") +const { applyPlan, approvePlan, invokeMcpTool } = await import("./runtime.js") const app = await buildApp({ startScheduler: false }) try { @@ -177,6 +177,54 @@ try { (SELECT count(*)::int FROM mcp_requests WHERE tool='protocol:unauthorized') AS unauthorized, (SELECT max(attempts)::int FROM mcp_unauthorized_buckets) AS attempts`) assert.deepEqual(counts, { denied: 1, unauthorized: 1, attempts: 8 }) + + const serviceOwner = { ...actor, permissions: [...actor.permissions, "mm:mcp:admin", "mm:dashboard:read"] } + const settings = mcpConnectionCreateSchema.parse({ name: "Service rate fixture", authority: "service", mode: "autonomous", expiresAt: null, + permissions: ["mm:dashboard:read"], scope: { serverIds: [], operationIds: ["capabilities.read", "events.batch.append"], allowGlobal: true }, + limits: { callsPerMinute: 1, concurrentReads: 1, mutationsPerHour: 1 } }) + const service = await createMcpConnection(settings, serviceOwner) + const servicePrincipal = await authenticateMcpToken(service.token) + const serviceId = service.connection.id + await pool.query(`INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) + SELECT $1,kind,date_trunc(period,now())+window_shift,10 FROM (VALUES + ('calls','minute',interval '0'),('protocol','minute',interval '0'),('mutations','hour',interval '0'), + ('calls','minute',interval '1 minute'),('protocol','minute',interval '1 minute'),('mutations','hour',interval '1 hour') + ) AS slots(kind,period,window_shift)`, [serviceId]) + const leaseId = randomUUID() + await pool.query("INSERT INTO mcp_read_leases(id,connection_id,expires_at) VALUES($1,$2,now()+interval '2 minutes')", [leaseId, serviceId]) + const serviceRequest = () => app.inject({ method: "POST", url: "/mcp", headers: { + authorization: `Bearer ${service.token}`, accept: "application/json, text/event-stream", "mcp-protocol-version": "2025-06-18", + }, payload: { jsonrpc: "2.0", id: 1, method: "tools/list", params: {} } }) + try { + assert.equal((await serviceRequest()).statusCode, 429) + await assert.rejects(invokeMcpTool(servicePrincipal, "mm_capabilities_get", {}), { code: "RATE_LIMITED" }) + const updated = await updateMcpConnection(serviceId, { ...settings, unlimitedRates: true }, serviceOwner) + assert.equal(updated.policy.unlimitedRates, true) + assert.equal((await authenticateMcpToken(service.token)).credentialId, servicePrincipal.credentialId) + assert.equal((await serviceRequest()).statusCode, 200) + await invokeMcpTool(servicePrincipal, "mm_capabilities_get", {}) + const eventType = `service-rate-fixture-${randomUUID()}` + const plan = await invokeMcpTool(servicePrincipal, "mm_events_batch_append_plan", { events: [ + { level: "info", eventType, sourceModule: "system", title: "Service rate fixture", message: "Synthetic journal entry" }, + ] }) as { planId: string; state: string } + assert.equal(plan.state, "awaiting-approval") + await approvePlan(plan.planId, serviceOwner, true) + const operation = await applyPlan(servicePrincipal, plan.planId, randomUUID()) + let outcome: string | undefined + for (let i = 0; i < 250; i++) { + outcome = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operation.id])).rows[0]?.state + if (outcome && !["pending", "running"].includes(outcome)) break + await delay(20) + } + assert.equal(outcome, "applied") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM events WHERE event_type=$1", [eventType])).rows[0].n, 1) + const restored = await updateMcpConnection(serviceId, settings, serviceOwner) + assert.equal(restored.policy.unlimitedRates, undefined) + assert.equal((await serviceRequest()).statusCode, 429) + await assert.rejects(invokeMcpTool(servicePrincipal, "mm_capabilities_get", {}), { code: "RATE_LIMITED" }) + } finally { + await pool.query("DELETE FROM mcp_read_leases WHERE id=$1", [leaseId]) + } console.log("MCP request accounting respects the backup/restore lock independently of the partition lease") } finally { await app.close() diff --git a/backend/src/modules/mcp/protocol-limits.ts b/backend/src/modules/mcp/protocol-limits.ts index 3a9e9d81..1fb7e8f6 100644 --- a/backend/src/modules/mcp/protocol-limits.ts +++ b/backend/src/modules/mcp/protocol-limits.ts @@ -1,6 +1,7 @@ import { randomUUID } from "node:crypto" import { requestAccountingWrite, validatePrincipal, type McpPrincipal } from "./store.js" import { accessGeneration, tokenDigest } from "./security.js" +import { hasUnlimitedMcpRates } from "./policy.js" export async function reserveProtocolEnvelope(principal: McpPrincipal) { return requestAccountingWrite(async client => { @@ -9,8 +10,8 @@ export async function reserveProtocolEnvelope(principal: McpPrincipal) { const connection = await validatePrincipal(principal, client) // Protocol negotiation and metadata have their own cap; tools retain their // stricter policy budget. A single metadata endpoint cannot bypass limits. - const limit = Math.max(10, Math.min(3000, connection.policy.limits.callsPerMinute * 3)) - const { rows } = await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'protocol',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+1 WHERE mcp_budget_buckets.used<$2 RETURNING used", [connection.id, limit]) + const limit = hasUnlimitedMcpRates(connection.policy) ? null : Math.max(10, Math.min(3000, connection.policy.limits.callsPerMinute * 3)) + const { rows } = await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'protocol',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used::bigint+1,2147483647) WHERE $2::integer IS NULL OR mcp_budget_buckets.used<$2 RETURNING used", [connection.id, limit]) if (!rows.length) { const { rows: [denied] } = await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'protocol-denied',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used+1,2147483647) RETURNING used", [connection.id]) if (denied.used === 1) await client.query("INSERT INTO mcp_requests(id,connection_id,actor_id,tool,status,input,error_code) VALUES($1,$2,$3,'protocol:rate-limit','denied','{}','RATE_LIMITED')", [randomUUID(), connection.id, connection.owner_id]) diff --git a/backend/src/modules/mcp/routing-reservation-store.ts b/backend/src/modules/mcp/routing-reservation-store.ts index 5cdf522b..e1a7f4ef 100644 --- a/backend/src/modules/mcp/routing-reservation-store.ts +++ b/backend/src/modules/mcp/routing-reservation-store.ts @@ -18,6 +18,7 @@ import { routingReservationDeadline, transitionRoutingReservation, type RoutingR import { routingReservationTransaction, type RoutingReservationConnect } from "./routing-reservation-transaction.js" import { lockRoutingReservationAdmissionSource } from "./routing-reservation-runtime.js" import type { StoredPlan } from "./store.js" +import { hasUnlimitedMcpRates } from "./policy.js" const uuid = z.string().uuid(), proof = z.string().regex(/^[a-f0-9]{64}$/), stamp = z.string().datetime() const principalSchema = z.object({ connectionId: uuid, credentialId: uuid, credentialExpiresAt: stamp }).strict() @@ -271,7 +272,8 @@ async function reserveWithinOwner(db: PoolClient, operationId: string) { // Legacy overlap stays charged, including allocations predating audit anchors. const charges = await readHistoricalChargesWithinOwner(db, value.authority.packet.profileId, connection, createdAt, deadline) if (charges.totalCharges + 2 > value.profile.maxSwitchesPerHour) throw fail("Insufficient durable profile switch slots") - const budget = await db.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) SELECT $1,'mutations',$2,2 WHERE $3::int>=2 ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+2 WHERE mcp_budget_buckets.used<=$3::int-2 RETURNING used", [connection.id, budgetBucket, connection.policy.limits.mutationsPerHour]) + const limit = hasUnlimitedMcpRates(connection.policy) ? null : connection.policy.limits.mutationsPerHour + const budget = await db.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) SELECT $1,'mutations',$2,2 WHERE $3::int IS NULL OR $3::int>=2 ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used::bigint+2,2147483647) WHERE $3::int IS NULL OR mcp_budget_buckets.used<=$3::int-2 RETURNING used", [connection.id, budgetBucket, limit]) if (budget.rowCount !== 1) throw fail("Insufficient connection mutation budget for forward and inverse") const binding = bindingSchema.parse({ revision: 1, id: randomUUID(), planId: value.row.plan_id, operationId, stepIndex: 0, sourceReceiptId: value.input.receiptId, inverseReceiptId: randomUUID(), profileId: value.authority.packet.profileId, profileVersion: value.authority.packet.profileVersion, principal: value.principal, diff --git a/backend/src/modules/mcp/routing-reservation.sql-helper.ts b/backend/src/modules/mcp/routing-reservation.sql-helper.ts index 2b3903b9..793fa1b4 100644 --- a/backend/src/modules/mcp/routing-reservation.sql-helper.ts +++ b/backend/src/modules/mcp/routing-reservation.sql-helper.ts @@ -3,6 +3,7 @@ import { randomUUID } from "node:crypto" import { basename, dirname, join, resolve } from "node:path" import { readFile, writeFile } from "node:fs/promises" import type { PoolClient } from "pg" +import { MCP_SERVICE_EXPIRATION } from "@mmapp/contracts/mcp" /** Actual SQL/crypto numbered-schema acceptance only; no public tool registration, * native callback, controller tick, reservation refund or fixture DELETE. */ @@ -49,11 +50,12 @@ export async function acceptRoutingReservationSql(options: { runtimeAdmission?: ids.push(...(await pool.query("INSERT INTO servers(name,host,port,enabled,username,password,use_ssl,verify_ssl) VALUES($1,'127.0.0.1',9,true,'SYNTHETIC','SYNTHETIC',false,true) RETURNING id", ["reservation-synthetic-" + randomUUID()])).rows.map(row => Number(row.id))) await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") const contextSources = (await createRoutingSourceContext(await readRoutingEndpoints(ids))).expected - const fixture = async (mutations = 10, maxSwitches = 3, autoRecovery = true) => { + const fixture = async (mutations = 10, maxSwitches = 3, autoRecovery = true, unlimitedRates = false) => { const connectionId = randomUUID(), credentialId = randomUUID(), profileId = randomUUID(), ownerId = "reservation-owner-" + randomUUID() const now = Date.now(), expiresAt = new Date(now + 600000).toISOString(), generation = await security.accessGeneration() const permissions = ["mm:network:read", "mm:network:write", "mm:filters:write"] - const policy = mcpConnectionPolicySchema.parse({ id: connectionId, version: 1, status: "active", mode: "approval", permissions, expiresAt, + const policy = mcpConnectionPolicySchema.parse({ id: connectionId, version: 1, status: "active", permissions, + ...(unlimitedRates ? { authority: "service", mode: "autonomous", expiresAt: MCP_SERVICE_EXPIRATION, unlimitedRates: true } : { mode: "approval", expiresAt }), scope: { serverIds: ids, operationIds: ["routing.analyze", "routing.community.switch", "routing.path.verify", "operations.recover"], allowGlobal: false }, limits: { callsPerMinute: 100, mutationsPerHour: mutations, concurrentReads: 4, serversPerPlan: 2, objectsPerPlan: 20 } }) await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'synthetic reservation',$2,$3::jsonb,$4::jsonb,$5,$6,$7)", [connectionId, ownerId, JSON.stringify(permissions), JSON.stringify(policy), generation, expiresAt, new Date(now - 1000)]) @@ -295,6 +297,13 @@ export async function acceptRoutingReservationSql(options: { runtimeAdmission?: assert.equal(await used(f.connectionId), 2) names.push("short by one rolling-hour profile slot across source operations") } + { + const f = await fixture(1, 3, true, true), first = await f.makeSource(), second = await f.makeSource() + assert.equal((await store.reserve(first.operationId)).created, true) + await assert.rejects(store.reserve(second.operationId), /profile switch slots/) + assert.equal(await used(f.connectionId), 2) + names.push("unlimited service connection still obeys the routing profile switch limit") + } { const f = await fixture(10, 3), source = await f.makeSource() await pool.query("UPDATE mcp_routing_profiles SET state=jsonb_build_object('switches',jsonb_build_array(to_char(clock_timestamp() AT TIME ZONE 'UTC','YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"'),to_char(clock_timestamp() AT TIME ZONE 'UTC','YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"'))) WHERE id=$1", [f.profileId]) diff --git a/backend/src/modules/mcp/runtime.ts b/backend/src/modules/mcp/runtime.ts index 4fa21bc9..d8f4fbca 100644 --- a/backend/src/modules/mcp/runtime.ts +++ b/backend/src/modules/mcp/runtime.ts @@ -31,7 +31,7 @@ import { readPollRuntimePolicySource, type PollRuntimePolicySource } from "./pol import { withVerifiedConfigurationChange, assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" import { beginMcpOperationAdmission, insertMcpOperationAdmission, retireMcpOperationAdmission, withMcpOperationExecutionOrigin, stripMcpOperationExecutionPrivateResult, type McpOperationAdmission } from "./operation-execution-origin.js" import { createOspfOwnerSourceGuard, verifyOspfOwnerGraph, verifyOspfOwnerGraphAtPhase } from "./ospf-owner-graph.js" -import { evaluateMcpPolicy, hasUnattendedPlanAuthority, operationApprovalRequirement } from "./policy.js" +import { evaluateMcpPolicy, hasUnattendedPlanAuthority, hasUnlimitedMcpRates, operationApprovalRequirement } from "./policy.js" import { findAction, findActionByOperation } from "./actions.js" import { refuseExpiredOperationReplay } from "./operation-replay-tombstones.js" import { accessGeneration, decryptSecret, encryptSecret, fingerprint, privateSecretFingerprint, McpError, redact } from "./security.js" @@ -138,7 +138,7 @@ async function reserveCall(principal: McpPrincipal, toolName: string, input: Rec const result = evaluateMcpPolicy(evidence) await client.query("INSERT INTO mcp_requests(id,connection_id,actor_id,tool,server_ids,status,input,error_code) VALUES($1,$2,$3,$4,$5,$6,$7::jsonb,$8)", [requestId, connection.id, connection.owner_id, toolName, evidence.target.scope === "servers" ? evidence.target.serverIds : [], result.decision === "denied" ? "denied" : "running", JSON.stringify(redact(input)), result.decision === "denied" ? result.reasons[0]?.code : null]) if (result.decision !== "denied") { - await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'calls',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+1", [connection.id]) + await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'calls',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used::bigint+1,2147483647)", [connection.id]) if (action.operation.effect === "read") await client.query("INSERT INTO mcp_read_leases(id,connection_id,expires_at) VALUES($1,$2,now()+interval '2 minutes')", [requestId, connection.id]) await client.query("UPDATE mcp_connections SET last_used_at=now() WHERE id=$1", [connection.id]) } @@ -461,8 +461,8 @@ async function admitPlan(principal: McpPrincipal, planId: string, idempotencyKey await assertOriginalMcpTemporaryGrant(client,connection.id,principal.credentialId,originalServiceGrant) } const mutationCost = reservedRouting ? 2 : changeMutationCost(evidence) + (evidence.recovery ? 1 : 0) - if (input.usage.mutationsInHour + mutationCost > connection.policy.limits.mutationsPerHour) throw new McpError("BUDGET_EXCEEDED", "Недостаточно бюджета для всех шагов плана", 429) - if (!reservedRouting) await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'mutations',date_trunc('hour',now()),$2) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+EXCLUDED.used", [connection.id, mutationCost]) + if (!hasUnlimitedMcpRates(connection.policy) && input.usage.mutationsInHour + mutationCost > connection.policy.limits.mutationsPerHour) throw new McpError("BUDGET_EXCEEDED", "Недостаточно бюджета для всех шагов плана", 429) + if (!reservedRouting) await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'mutations',date_trunc('hour',now()),$2) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used::bigint+EXCLUDED.used,2147483647)", [connection.id, mutationCost]) if (delegated) { // Execution receipts use the delegating owner's ID. Object protections still require a human decision. plan.evidence.approvalSource = "connection-policy" diff --git a/backend/src/services/traffic-flow-analytics.test.ts b/backend/src/services/traffic-flow-analytics.test.ts index 4143db97..44c9120f 100644 --- a/backend/src/services/traffic-flow-analytics.test.ts +++ b/backend/src/services/traffic-flow-analytics.test.ts @@ -118,6 +118,20 @@ try { assert.ok(filtered.bytes >= 12_000) assert.equal(filtered.ifaces[0]?.name, "ether1") + const egress = await buildFlowAnalytics({ minutes: 5, serverId, iface: "ether2" }) + assert.equal(egress.bytes, 12_000) + assert.deepEqual(egress.interfaces.map(row => [row.id, row.bytes]), [["ether2", 12_000]]) + assert.equal(egress.conversationsList[0]?.accountedIface, "ether2") + assert.equal(egress.paths?.[0]?.ifaces, "ether1, ether2") + assert.equal(egress.conversationsList[0]?.dstCountry, "unknown") + assert.equal(egress.mapEdges?.[0]?.toCountry, "unknown") + for (const rows of [egress.applications, egress.categories, egress.services, egress.asns, egress.countries, + egress.mapEdges, egress.protocols, egress.sources, egress.destinations, egress.paths, egress.conversationsList, egress.interfaces]) { + assert.equal(rows?.reduce((sum, row) => sum + row.bytes, 0), egress.bytes) + } + assert.equal(egress.asns?.[0]?.id, "unknown") + assert.equal(egress.countries?.[0]?.percent, 100) + const miss = await buildFlowAnalytics({ minutes: 5, serverId, iface: "wg-flow" }) assert.equal(miss.conversations, 0) @@ -175,7 +189,7 @@ try { assert.equal(deduped.bytes, 12_000) assert.equal(deduped.conversations, 1) assert.equal(deduped.dedupApplied, true) - assert.equal(deduped.interfaces.length, 2) + assert.deepEqual(deduped.interfaces.map(row => [row.id, row.bytes]), [["ether1", 12_000]]) } finally { resetFlowRingsForTests() resetIfaceCacheForTests() @@ -287,6 +301,7 @@ try { ]) const degraded = await buildFlowAnalytics({ minutes: 5, serverId, skipHeavy: true }) assert.equal(degraded.degraded, true) + assert.equal(degraded.conversations, 1) assert.equal(degraded.conversationsList.length, 0) assert.ok((degraded.bytes ?? 0) >= 12_000) const exporters = await listFlowExporters(5) diff --git a/backend/src/services/traffic-flow-analytics.ts b/backend/src/services/traffic-flow-analytics.ts index 821d920a..4187a624 100644 --- a/backend/src/services/traffic-flow-analytics.ts +++ b/backend/src/services/traffic-flow-analytics.ts @@ -49,7 +49,7 @@ export interface FlowAnalyticsQuery { serverId?: number userId?: string iface?: string - /** Default true: один 5-tuple = max байт по ifaces. */ + /** Default true: один 5-tuple за минуту = max байт по ifaces. */ dedup?: boolean /** Default true: скрыть GRE/WG между клиентами JH. */ excludeMesh?: boolean @@ -305,25 +305,25 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF else if (plane === "client_mesh") bytesMesh += r.bytes if (!shouldKeepPlane(plane, { excludeMesh, excludeOverlay })) continue matched.push(r) - - const ifaceKey = resolved.name - const prevIf = ifacesMap.get(ifaceKey) ?? { bytes: 0, packets: 0, index: resolved.index } - prevIf.bytes += r.bytes - prevIf.packets += r.packets - ifacesMap.set(ifaceKey, prevIf) } const working = wantDedup ? dedupFlowRowsMaxBytes(matched) : matched const conversationsRaw = new Set(matched.map((r) => `${flowTupleKey(r)}|${r.inIface}`)).size + const conversations = wantDedup ? new Set(working.map(flowTupleKey)).size : conversationsRaw let totalBytes = 0 let totalPackets = 0 for (const r of working) { const resolved = resolveIfaceName(r.serverId, r.inIface) + const outResolved = resolveIfaceName(r.serverId, r.outIface) + const ingressAccounts = ifaceFilter || allow ? matchesIngress(r) : resolved.name !== "—" + const accounted = ingressAccounts ? resolved : outResolved + const ifaceTotal = ifacesMap.get(accounted.name) ?? { bytes: 0, packets: 0, index: accounted.index } + ifaceTotal.bytes += r.bytes + ifaceTotal.packets += r.packets + ifacesMap.set(accounted.name, ifaceTotal) totalBytes += r.bytes totalPackets += r.packets - srcs.add(r.src) - dsts.add(r.dst) const destMeta = resolveInternetDest({ src: r.src, dst: r.dst, @@ -332,6 +332,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF dstPort: r.dstPort, serverId: r.serverId, inIface: resolved.name, + outIface: outResolved.name, topo, natSrc: r.natSrc, natDst: r.natDst, @@ -339,28 +340,27 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF natDstPort: r.natDstPort, }) const ep = destMeta.endpoints + const src = ep.packetSrc || r.src + const dst = ep.packetDst || r.dst + srcs.add(src) + dsts.add(dst) if (destMeta.dest) peers.add(destMeta.dest) const app = applicationName(r.proto, ep.peerPort || r.dstPort, ep.otherPort || r.srcPort) const ripe = destMeta.ripe const classified = destMeta.classified bump(applications, app, r.bytes, r.packets) bump(protocols, protoName(r.proto), r.bytes, r.packets) - bump(sources, r.src, r.bytes, r.packets) - bump(destinations, r.dst, r.bytes, r.packets) + bump(sources, src, r.bytes, r.packets) + bump(destinations, dst, r.bytes, r.packets) bump(categories, classified.category, r.bytes, r.packets) bump(services, classified.service, r.bytes, r.packets) - if (ripe?.ok && ripe.asn) { - const asnId = String(ripe.asn) - const asnLabel = ripe.holder ? `AS${ripe.asn} ${ripe.holder}` : `AS${ripe.asn}` - bump(asns, asnId, r.bytes, r.packets, asnLabel) - } - const dstCountry = destMeta.country && destMeta.country !== "unknown" ? destMeta.country : "" - if (dstCountry) { - bump(countries, dstCountry, r.bytes, r.packets) - } + const asnId = destMeta.asn ? String(destMeta.asn) : "unknown" + const asnLabel = destMeta.asn ? `AS${destMeta.asn}${ripe?.holder ? ` ${ripe.holder}` : ""}` : "ASN неизвестен" + bump(asns, asnId, r.bytes, r.packets, asnLabel) + const dstCountry = destMeta.country || "unknown" + bump(countries, dstCountry, r.bytes, r.packets, dstCountry === "unknown" ? "Страна неизвестна" : undefined) if (!skipHeavy) { - const outResolved = resolveIfaceName(r.serverId, r.outIface) const plane = classifyFlowPlane({ src: r.src, dst: r.dst, @@ -370,11 +370,13 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF inIface: resolved.name, outIface: outResolved.name, }, topo.plane) - const client = resolveClient(topo, r.serverId, resolved.name) + const client = resolveClient(topo, r.serverId, resolved.name) ?? resolveClient(topo, r.serverId, outResolved.name) const en = resolveEn(topo, r.nextHop, outResolved.name) - const ckey = wantDedup - ? flowTupleKey(r) - : `${flowTupleKey(r)}|${r.inIface}` + const outIface = outResolved.name === "—" ? "" : outResolved.name + const pathKey = JSON.stringify([client?.userId ?? "unknown", r.serverId, en?.id ?? "", dst, resolved.name, outIface, + classified.service, classified.category, plane]) + const ckey = JSON.stringify([flowTupleKey(r), pathKey, accounted.name, app, asnId, dstCountry, ep.internetPeer, + ep.clientIp, ep.peerPort, ep.direction, r.nextHop]) const prev = conv.get(ckey) if (prev) { prev.rawBytes += r.bytes @@ -386,8 +388,8 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF conv.set(ckey, { serverId: String(r.serverId), serverName: nameById.get(r.serverId) ?? String(r.serverId), - src: ep.packetSrc || r.src, - dst: ep.packetDst || r.dst, + src, + dst, proto: r.proto, protoName: protoName(r.proto), srcPort: r.srcPort, @@ -397,13 +399,14 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF bps: 0, inIface: resolved.name, inIfaceIndex: resolved.index, - outIface: outResolved.name !== "—" ? outResolved.name : undefined, + outIface: outIface || undefined, + accountedIface: accounted.name, nextHop: r.nextHop || undefined, application: app, category: classified.category, service: classified.service, - dstCountry: dstCountry || undefined, - dstAsn: ripe?.asn || undefined, + dstCountry, + dstAsn: destMeta.asn || undefined, clientId: client?.userId, clientName: client?.name, clientIp: ep.clientIp || undefined, @@ -419,7 +422,6 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF }) } - const pathKey = `${client?.userId || "unknown"}|${r.serverId}|${en?.id || ""}|${r.dst}|${resolved.name}` const pathPrev = pathAcc.get(pathKey) if (pathPrev) { pathPrev.bytes += r.bytes @@ -429,14 +431,14 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF id: pathKey, clientId: client?.userId || "unknown", clientName: client?.name || "Неизвестный клиент", - ifaces: client ? [...(topo.clientIfaces.get(r.serverId) ?? [resolved.name])].join(", ") : resolved.name, + ifaces: [...new Set([resolved.name, outIface].filter(name => name && name !== "—"))].join(", ") || "—", serverId: String(r.serverId), serverName: nameById.get(r.serverId) ?? String(r.serverId), inIface: resolved.name, - outIface: outResolved.name !== "—" ? outResolved.name : "", + outIface, enId: en ? String(en.id) : "", enName: en?.name || "", - dst: r.dst, + dst, service: classified.service, category: classified.category, plane, @@ -446,29 +448,25 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF }) } - const toCountry = dstCountry - if (toCountry) { - const fromCountry = countryById.get(r.serverId) || "UN" - const ekey = `${r.serverId}|${toCountry}` - let edge = edgeAcc.get(ekey) - if (!edge) { - edge = { - fromId: String(r.serverId), - fromLabel: nameById.get(r.serverId) ?? String(r.serverId), - fromCountry, - toCountry, - toAsn: ripe?.asn ?? 0, - category: classified.category, - bytes: 0, - bps: 0, - catBytes: new Map(), - } - edgeAcc.set(ekey, edge) + const ekey = `${r.serverId}|${dstCountry}` + let edge = edgeAcc.get(ekey) + if (!edge) { + edge = { + fromId: String(r.serverId), + fromLabel: nameById.get(r.serverId) ?? String(r.serverId), + fromCountry: countryById.get(r.serverId) || "UN", + toCountry: dstCountry, + toAsn: destMeta.asn, + category: classified.category, + bytes: 0, + bps: 0, + catBytes: new Map(), } - edge.bytes += r.bytes - if (ripe?.asn) edge.toAsn = ripe.asn - edge.catBytes.set(classified.category, (edge.catBytes.get(classified.category) ?? 0) + r.bytes) + edgeAcc.set(ekey, edge) } + edge.bytes += r.bytes + if (edge.toAsn !== destMeta.asn) edge.toAsn = 0 + edge.catBytes.set(classified.category, (edge.catBytes.get(classified.category) ?? 0) + r.bytes) } } @@ -494,10 +492,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF const ring = ringServer && !ifaceFilter && !allow && excludeOverlay ? getRingMbps(ringServer) : null // The live ring covers about one minute; historical charts must use the requested window. - // Keep the winning interface's individual minute buckets when deduplicating. - const chosenIfaces = wantDedup ? new Map(working.map(row => [flowTupleKey(row), row.inIface])) : null - const chartRows = chosenIfaces ? matched.filter(r => chosenIfaces.get(flowTupleKey(r)) === r.inIface) : matched - const fromBuckets = seriesFromRows(chartRows, q.minutes, row => { + const fromBuckets = seriesFromRows(working, q.minutes, row => { if (ifaceFilter || allow) { const ingress = matchesIngress(row), egress = matchesEgress(row) if (ingress && egress) { @@ -521,7 +516,6 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF bps: (v.bytes * 8) / windowSec, })) - const ifaceRawBytes = [...ifacesMap.values()].reduce((a, v) => a + v.bytes, 0) || 1 const listener = getFlowListenerState() const mapEdges: FlowMapEdge[] = [...edgeAcc.values()] .map((e) => { @@ -548,7 +542,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF .slice(0, top) // Filtered and overlay-inclusive rates must use the same records as their totals. - const recentBytes = ring ? 0 : chartRows.reduce((sum, row) => sum + (row.bucketAt === previousMinute ? row.bytes : 0), 0) + const recentBytes = ring ? 0 : working.reduce((sum, row) => sum + (row.bucketAt === previousMinute ? row.bytes : 0), 0) const bpsNow = ring ? (!fromBuckets.undirectedBytes && (ring.rxNow + ring.txNow) * 1_000_000) || (totalBytes * 8) / windowSec : (recentBytes * 8) / 60 || (totalBytes * 8) / windowSec @@ -570,7 +564,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF bpsNow, bytes: totalBytes, packets: totalPackets, - conversations: conv.size, + conversations, conversationsRaw, uniqueSrc: srcs.size, uniqueDst: dsts.size, @@ -588,7 +582,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF bytes: v.bytes, packets: v.packets, bps: (v.bytes * 8) / windowSec, - percent: (v.bytes / ifaceRawBytes) * 100, + percent: (v.bytes / (totalBytes || 1)) * 100, })).sort((a, b) => b.bytes - a.bytes), asns: topN(asns, windowSec, top), countries: topN(countries, windowSec, top), diff --git a/backend/src/services/traffic-flow-dedup.test.ts b/backend/src/services/traffic-flow-dedup.test.ts index 1cdd050c..7eafec4e 100644 --- a/backend/src/services/traffic-flow-dedup.test.ts +++ b/backend/src/services/traffic-flow-dedup.test.ts @@ -39,6 +39,15 @@ assert.equal(across[0]?.bytes, 9_000) const minute = "2026-11-22T06:26:00.000Z" const nextMinute = "2026-11-22T06:27:00.000Z" +const beforeReroute = { ...a, bucketAt: minute } +const afterReroute = { ...b, bucketAt: nextMinute } +assert.deepEqual(dedupFlowRowsMaxBytes([beforeReroute, { ...b, bucketAt: minute }, afterReroute]), [ + beforeReroute, afterReroute, +]) +assert.deepEqual(dedupFlowRowsAcrossExporters( + [{ ...en, bucketAt: minute }, beforeReroute, afterReroute], + (x, y) => x.serverId === a.serverId ? x : y, +), [beforeReroute, afterReroute]) const gre = { ...a, bucketAt: minute, proto: 47, srcPort: 0, dstPort: 0 } const esp = { ...gre, proto: 50, bytes: gre.bytes + gre.packets * 40, inIface: "10" } const plain = { ...gre, dst: "198.51.100.3" } diff --git a/backend/src/services/traffic-flow-dedup.ts b/backend/src/services/traffic-flow-dedup.ts index 9dcaa21a..26a4862b 100644 --- a/backend/src/services/traffic-flow-dedup.ts +++ b/backend/src/services/traffic-flow-dedup.ts @@ -8,6 +8,7 @@ export interface FlowTupleRow { srcPort: number dstPort: number inIface: string + bucketAt?: string bytes: number packets: number } @@ -31,7 +32,7 @@ export function dedupFlowRowsAcrossExporters( ): T[] { const byConv = new Map() for (const row of rows) { - const key = flowConversationKey(row) + const key = `${row.bucketAt ?? ""}|${flowConversationKey(row)}` const prev = byConv.get(key) if (!prev) { byConv.set(key, row) @@ -42,29 +43,27 @@ export function dedupFlowRowsAcrossExporters( return [...byConv.values()] } -function ifaceKey(r: FlowTupleRow): string { - return `${flowTupleKey(r)}|${r.inIface}` +function bucketTupleKey(r: FlowTupleRow): string { + return `${r.bucketAt ?? ""}|${flowTupleKey(r)}` } /** - * Один 5-tuple на двух ifIndex — это один поток: сначала сумма по бакетам/iface, - * затем max байт между интерфейсами (не sum). + * Сравниваем копии 5-tuple в одном бакете: между бакетами интерфейс может меняться. */ export function dedupFlowRowsMaxBytes(rows: T[]): T[] { const byIface = new Map() for (const row of rows) { - const key = ifaceKey(row) + const key = `${bucketTupleKey(row)}|${row.inIface}` const prev = byIface.get(key) if (!prev) { - byIface.set(key, { ...row }) + byIface.set(key, row) continue } - prev.bytes += row.bytes - prev.packets += row.packets + byIface.set(key, { ...prev, bytes: prev.bytes + row.bytes, packets: prev.packets + row.packets }) } const byTuple = new Map() for (const row of byIface.values()) { - const key = flowTupleKey(row) + const key = bucketTupleKey(row) const prev = byTuple.get(key) if (!prev || row.bytes > prev.bytes) byTuple.set(key, row) } diff --git a/backend/src/services/traffic-flow-dest.ts b/backend/src/services/traffic-flow-dest.ts index ef7d1de8..3f255504 100644 --- a/backend/src/services/traffic-flow-dest.ts +++ b/backend/src/services/traffic-flow-dest.ts @@ -53,17 +53,16 @@ export function destCtxForIface( topo: FlowTopology | null | undefined, serverId: number, inIface: string, - nat?: Pick, + nat?: Pick & { outIface?: string }, resolveIface=canonicalFactIface, ): InternetDestCtx { const name = resolveIface(serverId, inIface) || String(inIface ?? "").trim() + const outName = nat?.outIface ? resolveIface(serverId, nat.outIface) : "" return { ours: flowOursHosts(topo), boundClient: Boolean( - topo && name && ( - resolveClient(topo, serverId, name) - || topo.clientIfaces.get(serverId)?.has(name) - ), + topo && [name, outName].some(iface => iface && + (resolveClient(topo, serverId, iface) || topo.clientIfaces.get(serverId)?.has(iface))), ), natSrc: nat?.natSrc, natDst: nat?.natDst, @@ -117,6 +116,7 @@ export function resolveInternetDest(opts: { dstPort: number serverId: number inIface: string + outIface?: string topo?: FlowTopology | null natSrc?: string natDst?: string @@ -127,6 +127,7 @@ export function resolveInternetDest(opts: { classify?:typeof classifyFlowDst }): InternetDestMeta { const ctx = destCtxForIface(opts.topo, opts.serverId, opts.inIface, { + outIface: opts.outIface, natSrc: opts.natSrc, natDst: opts.natDst, natSrcPort: opts.natSrcPort, diff --git a/backend/src/services/traffic-flow-engine.ts b/backend/src/services/traffic-flow-engine.ts index 3aed9978..0d40510d 100644 --- a/backend/src/services/traffic-flow-engine.ts +++ b/backend/src/services/traffic-flow-engine.ts @@ -434,7 +434,7 @@ export function queueParsedFlows(serverId: number, flows: ParsedFlowInput[]): vo for (const raw of flows) { const flow = normalizeParsedFlow(raw) const destMeta = resolveInternetDest({src:flow.src,dst:flow.dst,proto:flow.proto,srcPort:flow.srcPort,dstPort:flow.dstPort, - serverId,inIface:flow.inIface,topo,natSrc:flow.natSrc,natDst:flow.natDst,natSrcPort:flow.natSrcPort,natDstPort:flow.natDstPort}) + serverId,inIface:flow.inIface,outIface:flow.outIface,topo,natSrc:flow.natSrc,natDst:flow.natDst,natSrcPort:flow.natSrcPort,natDstPort:flow.natDstPort}) const classified=destMeta.classified,country=destMeta.country const seenIfaces = new Map() let interfaceUncertain = false diff --git a/backend/src/services/traffic-flow-facts-filter.ts b/backend/src/services/traffic-flow-facts-filter.ts index 02c08e78..3e9ce114 100644 --- a/backend/src/services/traffic-flow-facts-filter.ts +++ b/backend/src/services/traffic-flow-facts-filter.ts @@ -115,6 +115,7 @@ export function shouldWriteFlowFact(opts: { opts.srcPort, opts.dstPort, destCtxForIface(opts.topo, opts.serverId, inName, { + outIface: outName, natSrc: opts.natSrc, natDst: opts.natDst, natSrcPort: opts.natSrcPort, diff --git a/backend/src/services/traffic-flow-map-hops.test.ts b/backend/src/services/traffic-flow-map-hops.test.ts index 1792d9dc..09ba3e26 100644 --- a/backend/src/services/traffic-flow-map-hops.test.ts +++ b/backend/src/services/traffic-flow-map-hops.test.ts @@ -296,6 +296,8 @@ try { resetFlowMapHopsCacheForTests() const six = await buildFlowMapHops({ minutes: 5, minSharePct: 5 }) assert.equal(six.totalBytes, 10_000) + const byEgress = await buildFlowMapHops({ minutes: 5, serverId: 7, iface: "gre-jh-en", minSharePct: 5 }) + assert.equal(byEgress.totalBytes, six.totalBytes) assert.equal(six.namedBytes, 600) assert.equal(six.unclassifiedBytes, 9400) const google = six.services?.find((s) => s.id === "svc:google") @@ -317,6 +319,11 @@ try { const usPath = six.countryPaths?.find((p) => p.serviceId === "cc:us" && p.enId === "9") assert.ok(usPath) assert.equal(usPath.clientName, "Alice") + + ingestParsedFlowsForServerForTests(7, [{ ...payloadFlow("8.8.8.8", 2000), proto: 17, dstPort: 51820 }]) + const mixedPeer = await buildFlowMapHops({ minutes: 5, serverId: 7, excludeOverlay: false }) + assert.equal(mixedPeer.totalBytes, 12_000) + assert.equal(mixedPeer.namedBytes, 600, "WireGuard to the same peer is separate from the HTTPS service") } finally { resetFlowRingsForTests() resetIfaceCacheForTests() diff --git a/backend/src/services/traffic-flow-map-hops.ts b/backend/src/services/traffic-flow-map-hops.ts index 03f0f674..d773adf5 100644 --- a/backend/src/services/traffic-flow-map-hops.ts +++ b/backend/src/services/traffic-flow-map-hops.ts @@ -8,11 +8,11 @@ import { flowRowMatchesFilter } from "./traffic-flow-apps.js" import { OTHER_SERVICE, isNamedInternetService, mapCountryNodeId, mapCountryServiceNodeId, mapServiceNodeId, resolveRipeCountry } from "./traffic-flow-brands.js" import type { FlowIpMeta } from "./traffic-flow-ripe.js" import { refreshFlowCatalogInBackground } from "./traffic-flow-classify.js" -import { dedupFlowRowsAcrossExporters, dedupFlowRowsMaxBytes } from "./traffic-flow-dedup.js" +import { collapseGreEspRows, dedupFlowRowsAcrossExporters, dedupFlowRowsMaxBytes } from "./traffic-flow-dedup.js" import { getFlowListenerState, listFlowRowsForWindow } from "./traffic-flow-ingest.js" import { resolveIfaceName } from "./traffic-flow-ifaces.js" import { classifyFlowPlane, shouldKeepPlane } from "./traffic-flow-planes.js" -import { destCtxForIface, mapInternetBrand } from "./traffic-flow-dest.js" +import { destCtxForIface, flowRateDirection, mapInternetBrand } from "./traffic-flow-dest.js" import { resolveFlowEndpoints } from "./traffic-flow-ip.js" import { geoipReadersStatus, resolveFlowIp } from "./traffic-flow-geoip.js" import { getTrafficFlowSettingsRow } from "./traffic-flow-settings.js" @@ -65,6 +65,7 @@ interface FromAcc { } interface DstAcc { + dst: string bytes: number proto: number dstPort: number @@ -316,7 +317,7 @@ function buildCountryServiceGroups( } function countryDestFromRipe(ripe: FlowIpMeta | null, destKey: string): { id: string; label: string; category: string } { - if (!destKey || destKey === "__other__" || !ripe?.ok) { + if (!destKey || !ripe?.ok) { return { id: mapCountryNodeId(""), label: OTHER_SERVICE, category: MAP_COUNTRY_CATEGORY } } const iso = resolveRipeCountry(ripe.country, ripe.asn, ripe.holder) @@ -440,11 +441,20 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number } } + const matches = (serverId: number, iface: string) => + flowRowMatchesFilter({ serverId, inIface: iface }, resolveIfaceName(serverId, iface).name, q, allow) + const selected = raw.filter(r => matches(r.serverId, r.inIface) || matches(r.serverId, r.outIface)) + const { rows } = collapseGreEspRows(selected, r => { + if (ifaceFilter) { + const ingress = matches(r.serverId, r.inIface), egress = matches(r.serverId, r.outIface) + return ingress === egress ? null : ingress ? "rx" : "tx" + } + return flowRateDirection(r.serverId, r, topo, catalog.byId.get(r.serverId)?.host) + }) const matched = [] - for (const r of raw) { + for (const r of rows) { const resolved = resolveIfaceName(r.serverId, r.inIface) const outResolved = resolveIfaceName(r.serverId, r.outIface) - if (!flowRowMatchesFilter(r, resolved.name, q, allow)) continue const plane = classifyFlowPlane({ src: r.src, dst: r.dst, @@ -578,14 +588,17 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number srcPort: r.srcPort, dstPort: r.dstPort, ctx: destCtxForIface(topo, r.serverId, inName, { + outIface: outName, natSrc: r.natSrc, natDst: r.natDst, natSrcPort: r.natSrcPort, natDstPort: r.natDstPort, }), }) - const dest = ep.internetPeer - const destKey = dest || "__other__" + const dst = ep.internetPeer + const dstPort = ep.peerPort || r.dstPort + const srcPort = ep.otherPort || r.srcPort + const destKey = dst ? JSON.stringify([dst, r.proto, dstPort, srcPort]) : "" const client = resolveMapClient(topo, r.serverId, inName, outName) const prevDst = dstAcc.get(destKey) if (prevDst) { @@ -593,10 +606,11 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number bumpFrom(prevDst, String(r.serverId), r.bytes, client) } else { const acc: DstAcc = { + dst, bytes: r.bytes, proto: r.proto, - dstPort: ep.peerPort || r.dstPort, - srcPort: ep.otherPort || r.srcPort, + dstPort, + srcPort, fromBytes: new Map(), } bumpFrom(acc, String(r.serverId), r.bytes, client) @@ -644,9 +658,10 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number return id } - for (const [dst, acc] of dstAcc) { - const ripe = dst && dst !== "__other__" ? resolveFlowIp(dst) : null - const classified = dst && dst !== "__other__" + for (const acc of dstAcc.values()) { + const { dst } = acc + const ripe = dst ? resolveFlowIp(dst) : null + const classified = dst ? mapInternetBrand(dst, acc.proto, acc.dstPort, acc.srcPort, ripe) : { service: OTHER_SERVICE, category: OTHER_SERVICE } const svcId = mapServiceNodeId(classified.service) diff --git a/backend/src/services/traffic-flow-read-http.owned.test.ts b/backend/src/services/traffic-flow-read-http.owned.test.ts index eb96b2c5..ec441012 100644 --- a/backend/src/services/traffic-flow-read-http.owned.test.ts +++ b/backend/src/services/traffic-flow-read-http.owned.test.ts @@ -195,6 +195,12 @@ try{ assert.equal(analytics.bytesOverlay,40200,'Plain GRE and undirected ESP remain part of the volume') assert.equal(fromSeries(analytics.rxSeries),25900) assert.equal(fromSeries(analytics.txSeries),12400) + assert.deepEqual(analytics.interfaces.map(row=>[row.id,row.bytes]),[['ether1',40300]]) + assert.ok(analytics.conversationsList.every(row=>row.accountedIface==='ether1')) + for(const rows of [analytics.applications,analytics.categories,analytics.services,analytics.asns,analytics.countries, + analytics.mapEdges,analytics.protocols,analytics.sources,analytics.destinations,analytics.paths,analytics.conversationsList]){ + assert.equal(rows?.reduce((sum,row)=>sum+row.bytes,0),analytics.bytes) + } } assert.equal(sample.bpsOverlay,40200*8/300) }finally{ diff --git a/components/mcp/mcp-workspace.tsx b/components/mcp/mcp-workspace.tsx index ba042cf7..0d0805e7 100644 --- a/components/mcp/mcp-workspace.tsx +++ b/components/mcp/mcp-workspace.tsx @@ -6,7 +6,7 @@ import Link from "next/link" import { useTable, type ColumnDef } from "@tanstack/react-table" import { BotIcon, CheckIcon, CopyIcon, PlusIcon, RefreshCwIcon, ServerIcon, ShieldIcon, StopCircleIcon } from "lucide-react" import { toast } from "sonner" -import { mcpConnectionCreateSchema, mcpPageQuerySchema, mcpTargetSchema, type McpConnection, type McpOperationDescriptor, type McpPolicyDecision } from "@mmapp/contracts/mcp" +import { mcpConnectionCreateSchema, mcpLimitsSchema, mcpPageQuerySchema, mcpTargetSchema, type McpConnection, type McpOperationDescriptor, type McpPolicyDecision } from "@mmapp/contracts/mcp" import { routeAiControllerBlockReason, routeAiProfileSchema, type RouteAiProfile } from "@mmapp/contracts/mcp-routing" import { useDataSource } from "@/lib/data-source" import { can, getClaims, getToken, hasPermission } from "@/lib/auth" @@ -239,13 +239,15 @@ function ConnectionForm({ api, operations, editing, close, created }: { api: Mcp const [name, setName] = useState(editing?.name ?? ""), [mode, setMode] = useState(editing?.policy.mode ?? "observer") const [service, setService] = useState(editing?.policy.authority === "service") const [unattended, setUnattended] = useState(editing?.policy.allowUnattendedPlans === true) + const [unlimitedRates, setUnlimitedRates] = useState(editing?.policy.unlimitedRates === true) + const ratesDisabled = service && unlimitedRates const [selectAllServers, setSelectAllServers] = useState(false) const [serverIds, setServerIds] = useState(editing?.policy.scope.serverIds ?? []), [selected, setSelected] = useState(editing?.policy.scope.operationIds ?? []) const serverCatalog = useQuery(useCallback(signal => api.serverChoices(undefined, signal), [api])) const [global, setGlobal] = useState(editing?.policy.scope.allowGlobal ?? false), [danger, setDanger] = useState(editing?.policy.dangerousPolicy ?? "approval") const [dangerIds, setDangerIds] = useState(editing?.policy.autonomousDangerousOperationIds ?? []) const [expires, setExpires] = useState(() => editing?.policy.expiresAt.slice(0, 16) ?? new Date(Date.now() + 30 * 86400000).toISOString().slice(0, 16)) - const [limits, setLimits] = useState(editing?.policy.limits ?? { callsPerMinute: 60, concurrentReads: 4, mutationsPerHour: 10, serversPerPlan: 5, objectsPerPlan: 100 }) + const [limits, setLimits] = useState(() => editing?.policy.limits ?? mcpLimitsSchema.parse({})) const permissions = getClaims()?.permissions ?? [] const eligible = operations.filter(operation => operation.implementation === "available" && operation.requiredPermissions.every(permission => hasPermission(permissions, permission))) const selectedIds = new Set(selected) @@ -254,7 +256,9 @@ function ConnectionForm({ api, operations, editing, close, created }: { api: Mcp const save = async () => { setBusy(true); setError("") try { - const input = mcpConnectionCreateSchema.parse({ name, ...(service ? { authority: "service", mode: "autonomous", expiresAt: null, ...(unattended && { allowUnattendedPlans: true }) } : { mode, expiresAt: new Date(expires + "Z").toISOString() }), permissions: [...new Set(eligible.filter(operation => selected.includes(operation.id)).flatMap(operation => operation.requiredPermissions))], ...(selectAllServers && { selectAllServers: true }), scope: { serverIds: selectAllServers ? [] : serverIds, operationIds: selected, allowGlobal: global }, dangerousPolicy: service ? "approval" : danger, autonomousDangerousOperationIds: service || danger !== "explicit-allow" ? [] : autonomousDangerIds, limits }) + const previousLimits = editing?.policy.limits ?? mcpLimitsSchema.parse({}) + const savedLimits = ratesDisabled ? { ...previousLimits, serversPerPlan: limits.serversPerPlan, objectsPerPlan: limits.objectsPerPlan } : limits + const input = mcpConnectionCreateSchema.parse({ name, ...(service ? { authority: "service", mode: "autonomous", expiresAt: null, ...(unattended && { allowUnattendedPlans: true }), ...(ratesDisabled && { unlimitedRates: true }) } : { mode, expiresAt: new Date(expires + "Z").toISOString() }), permissions: [...new Set(eligible.filter(operation => selected.includes(operation.id)).flatMap(operation => operation.requiredPermissions))], ...(selectAllServers && { selectAllServers: true }), scope: { serverIds: selectAllServers ? [] : serverIds, operationIds: selected, allowGlobal: global }, dangerousPolicy: service ? "approval" : danger, autonomousDangerousOperationIds: service || danger !== "explicit-allow" ? [] : autonomousDangerIds, limits: savedLimits }) if (editing) { await api.update(editing.id, input); created() } else { const result = await api.create(input); created(result.token) } } catch (failure) { setError(errorText(failure)) } finally { setBusy(false) } @@ -270,14 +274,17 @@ function ConnectionForm({ api, operations, editing, close, created }: { api: Mcp

Выбор операций учитывает права владельца. Режим, список серверов и глобальный доступ задаются отдельно. Новые инструменты нужно добавлять вручную. Для применения планов выберите также инструменты просмотра и применения.

} - {step === 3 && <>
{Object.entries(limits).map(([key, value]) => )[key]}> setLimits(current => ({ ...current, [key]: Number(event.target.value) }))} />)}
{service ? <> + {step === 3 && <> + {service && } + {ratesDisabled &&

Вызовы, одновременные чтения и изменения в час без квот подключения. Общая ёмкость сервера и размер плана ограничены.

} +
{Object.entries(limits).filter(([key]) => !ratesDisabled || key === "serversPerPlan" || key === "objectsPerPlan").map(([key, value]) => )[key]}> setLimits(current => ({ ...current, [key]: Number(event.target.value) }))} />)}
{service ? <>

Действует для всех операций, выданных ключу. Клиент должен сначала показать точный план в чате. Сервер проверяет контрольную сумму плана, а публикацию сообщения обеспечивает клиент. Защита объектов сохраняется.

: <> setDanger(value as typeof danger)}>{danger === "explicit-allow" && <>

В автономном режиме выбранные здесь опасные операции смогут выполняться без подтверждения, если этого не требует защита объекта или сама операция.

}}} - {step === 4 &&

{name || "Без названия"} · {service ? "Сервисное, бессрочное" : modeLabels[mode]}

Серверы: {selectAllServers ? "все на момент сохранения" : serverIds.join(", ") || "не выбраны"}. Операций: {selected.length}. Глобальный доступ: {global ? "да" : "нет"}.

{service && unattended &&

Ручное подтверждение планов отключено. Клиент публикует план в чате перед применением.

}{!service && mode === "autonomous" && danger === "explicit-allow" &&

Опасных операций выбрано для автономного выполнения: {autonomousDangerIds.length}.

}

Защита объектов продолжает действовать независимо от автономных разрешений.

После сохранения проверьте конкретное действие в симуляторе на вкладке «Разрешения».

} + {step === 4 &&

{name || "Без названия"} · {service ? "Сервисное, бессрочное" : modeLabels[mode]}

Серверы: {selectAllServers ? "все на момент сохранения" : serverIds.join(", ") || "не выбраны"}. Операций: {selected.length}. Глобальный доступ: {global ? "да" : "нет"}.

{ratesDisabled &&

Квоты частоты для этого ключа отключены.

}{service && unattended &&

Ручное подтверждение планов отключено. Клиент публикует план в чате перед применением.

}{!service && mode === "autonomous" && danger === "explicit-allow" &&

Опасных операций выбрано для автономного выполнения: {autonomousDangerIds.length}.

}

Защита объектов продолжает действовать независимо от автономных разрешений.

После сохранения проверьте конкретное действие в симуляторе на вкладке «Разрешения».

}
{step < 4 ? : }
} diff --git a/components/mcp/object-permission-preview.tsx b/components/mcp/object-permission-preview.tsx index fcbf49de..bbe7c7c8 100644 --- a/components/mcp/object-permission-preview.tsx +++ b/components/mcp/object-permission-preview.tsx @@ -66,6 +66,6 @@ export function PolicyPreviewDecision({ result }: { result: McpPolicyDecision }) return
{result.decision === "allowed" ? "Разрешено" : result.decision === "denied" ? "Запрещено" : "Нужно подтверждение"} {result.reasons.length > 0 &&
    {result.reasons.map((item, index) =>
  • {item.message} ({item.code})
  • )}
} -

Лимиты подключения: {result.limits.callsPerMinute} вызовов/мин · {result.limits.concurrentReads} параллельных чтения · {result.limits.mutationsPerHour} изменений/ч · до {result.limits.serversPerPlan} серверов и {result.limits.objectsPerPlan} объектов в плане.

+

{result.unlimitedRates ? "Квоты частоты отключены" : `${result.limits.callsPerMinute} вызовов/мин · ${result.limits.concurrentReads} одновременных чтений · ${result.limits.mutationsPerHour} изменений/ч`} · до {result.limits.serversPerPlan} серверов и {result.limits.objectsPerPlan} объектов в плане.

} diff --git a/components/traffic/flow-analytics-panel.tsx b/components/traffic/flow-analytics-panel.tsx index 67a3123f..528c177e 100644 --- a/components/traffic/flow-analytics-panel.tsx +++ b/components/traffic/flow-analytics-panel.tsx @@ -97,25 +97,32 @@ export function FlowEntityCardView({ ) } -type SessionFilter = { - kind: "application" | "category" | "service" | "asn" | "country" | "protocol" | "source" | "destination" | "iface" | "client" | "en" +type BreakdownFilter = { + kind: "application" | "category" | "service" | "asn" | "country" | "protocol" | "source" | "destination" | "iface" value: string label: string + serverId?: string } +type SessionFilter = BreakdownFilter | { kind: "path"; path: FlowPathRow; label: string } function talkerMatchesFilter(row: FlowTalkerDto, filter: SessionFilter): boolean { switch (filter.kind) { case "application": return row.application === filter.value case "category": return row.category === filter.value case "service": return row.service === filter.value - case "asn": return String(row.dstAsn ?? "") === filter.value - case "country": return row.dstCountry === filter.value + case "asn": return String(row.dstAsn ?? "unknown") === filter.value + case "country": return (!filter.serverId || row.serverId === filter.serverId) && (row.dstCountry || "unknown") === filter.value case "protocol": return row.protoName === filter.value case "source": return row.src === filter.value case "destination": return row.dst === filter.value - case "iface": return row.inIface === filter.value - case "client": return (row.clientId || "unknown") === filter.value - case "en": return (row.enId || "") === filter.value + case "iface": return (row.accountedIface ?? row.inIface) === filter.value + case "path": { + const path = filter.path + return row.serverId === path.serverId && (row.clientId || "unknown") === path.clientId && + (row.enId || "") === path.enId && row.dst === path.dst && row.inIface === path.inIface && + (row.outIface || "") === path.outIface && row.service === path.service && + row.category === path.category && row.plane === path.plane + } } } @@ -141,7 +148,7 @@ function FlowBreakdownGrid({ cell: ({ row }) => (
- {country ? : null} + {country && /^[a-z]{2}$/i.test(row.original.id) ? : null} {row.original.label}
@@ -351,7 +358,7 @@ export function FlowAnalyticsDetail({ sessionFilter ? talkerMatchesFilter(row, sessionFilter) : true, ) - function pickBreakdown(kind: SessionFilter["kind"], row: FlowBreakdownRow) { + function pickBreakdown(kind: BreakdownFilter["kind"], row: FlowBreakdownRow) { setSessionFilter({ kind, value: row.id, label: row.label }) setSlice("sessions") } @@ -569,6 +576,9 @@ export function FlowAnalyticsDetail({

Аналитика потребления

{liveHint === "live" ? live : null}
+

+ Разрезы рассчитаны по сохранённой выборке NetFlow. В рейтингах до 50 крупнейших строк, доли рассчитаны от всей выборки. +

setSlice(String(v))} className="gap-3"> Приложения @@ -585,6 +595,7 @@ export function FlowAnalyticsDetail({ Интерфейсы +

Приложения определяются по протоколу и портам.

pickBreakdown("application", row)} />
@@ -602,8 +613,9 @@ export function FlowAnalyticsDetail({ { - setSessionFilter({ kind: "country", value: iso, label: iso }) + onSelectEdge={(edge) => { + setSessionFilter({ kind: "country", value: edge.toCountry, serverId: edge.fromId, + label: edge.toCountry === "unknown" ? "Страна неизвестна" : edge.toCountry }) setSlice("sessions") }} /> @@ -623,8 +635,8 @@ export function FlowAnalyticsDetail({ empty="Нет путей за период" onPick={(row) => { setSessionFilter({ - kind: "client", - value: row.clientId, + kind: "path", + path: row, label: `${row.clientName} → ${row.enName || row.dst}`, }) setSlice("sessions") diff --git a/components/traffic/flow-traffic-map.tsx b/components/traffic/flow-traffic-map.tsx index 71e7c574..16329082 100644 --- a/components/traffic/flow-traffic-map.tsx +++ b/components/traffic/flow-traffic-map.tsx @@ -34,10 +34,10 @@ function formatBytes(n: number): string { function FlowTrafficMap({ edges, - onSelectCountry, + onSelectEdge, }: { edges: FlowMapEdge[] - onSelectCountry?: (country: string) => void + onSelectEdge?: (edge: FlowMapEdge) => void }) { const [hover, setHover] = useState(null) @@ -77,7 +77,7 @@ function FlowTrafficMap({ {/^[a-z]{2}$/i.test(row.original.toCountry) ? : null} - {row.original.toCountry} + {row.original.toCountry === "unknown" ? "Страна неизвестна" : row.original.toCountry} {row.original.toAsn ? AS{row.original.toAsn} : null} ), @@ -120,7 +120,7 @@ function FlowTrafficMap({ if (!edges.length) { return (

- Страны подтянутся из кэша RIPEstat + Нет данных за период

) } @@ -150,10 +150,10 @@ function FlowTrafficMap({ className="cursor-pointer" onMouseEnter={() => setHover(id)} onMouseLeave={() => setHover(null)} - onClick={() => onSelectCountry?.(e.toCountry)} + onClick={() => onSelectEdge?.(e)} > - {`${e.fromLabel} → ${e.toCountry} · ${e.category} · ${formatBytes(e.bytes)}`} + {`${e.fromLabel} → ${e.toCountry === "unknown" ? "Страна неизвестна" : e.toCountry} · ${e.category} · ${formatBytes(e.bytes)}`} ) @@ -170,7 +170,7 @@ function FlowTrafficMap({ - {n.country} + {n.country === "unknown" ? "Без гео" : n.country} ))} @@ -185,8 +185,8 @@ function FlowTrafficMap({ onSelectCountry?.(row.toCountry)} + emptyMessage="Нет данных за период" + onRowClick={onSelectEdge} />
) diff --git a/docs/mcp-enable-guide.md b/docs/mcp-enable-guide.md index 11e893ec..41044e1a 100644 --- a/docs/mcp-enable-guide.md +++ b/docs/mcp-enable-guide.md @@ -123,7 +123,9 @@ В «Подключения» → «Выдать доступ» администратор MCP может выбрать «Сервисное — бессрочный автономный доступ». Выберите нужные серверы и операции. Доступ действует до отзыва и переживает истечение обычной сессии портала. По умолчанию опасные действия требуют ручного подтверждения. -На шаге 3 можно включить «Выполнять планы без ручного подтверждения». Режим действует на все выданные ключу операции. Списки серверов, права, лимиты и правила защиты объектов продолжают действовать. +На шаге 3 можно включить «Выполнять планы без ручного подтверждения». Режим действует на все выданные ключу операции. Списки серверов, права и правила защиты объектов продолжают действовать. + +Опция «Отключить квоты частоты для этого ключа» доступна при создании и редактировании сервисного подключения. Она снимает квоты на HTTP-запросы MCP, вызовы инструментов, одновременные чтения и изменения в час. В API ей соответствует `unlimitedRates: true`. Ключ менять не нужно. Ограничения размера плана и общей нагрузки сервера продолжают действовать. Если план содержит `announcement`, клиент сначала публикует отдельное сообщение в чате с ID плана, целями, изменениями и предупреждениями из `changes`. Затем вызывает `mm_change_apply` с `planId`, `idempotencyKey` и `announcedPlanDigest`, равным `announcement.digest`. Manager проверяет контрольную сумму сохранённого плана. Публикацию сообщения обеспечивает клиент. В аудите такой запуск отмечается как допуск по политике подключения. diff --git a/packages/contracts/src/mcp.ts b/packages/contracts/src/mcp.ts index 8f10d086..4e6f9cf1 100644 --- a/packages/contracts/src/mcp.ts +++ b/packages/contracts/src/mcp.ts @@ -36,6 +36,7 @@ export const mcpConnectionPolicySchema = z.object({ status: z.enum(["active", "paused", "revoked"]), authority: z.literal("service").optional(), allowUnattendedPlans: z.literal(true).optional(), + unlimitedRates: z.literal(true).optional(), expiresAt: instant, mode: mcpModeSchema, permissions: uniqueStrings, @@ -45,7 +46,7 @@ export const mcpConnectionPolicySchema = z.object({ limits: mcpLimitsSchema.default(() => mcpLimitsSchema.parse({})), }).strict().refine(policy => policy.authority === "service" ? policy.expiresAt === MCP_SERVICE_EXPIRATION && policy.mode === "autonomous" && policy.dangerousPolicy === "approval" && !policy.autonomousDangerousOperationIds.length - : !policy.allowUnattendedPlans, + : !policy.allowUnattendedPlans && !policy.unlimitedRates, "Некорректная политика сервисного подключения") export const mcpTargetSchema = z.discriminatedUnion("scope", [ @@ -111,6 +112,7 @@ export const mcpPolicyDecisionSchema = z.object({ decision: z.enum(["denied", "approval-required", "allowed"]), reasons: z.array(z.object({ code: mcpPolicyReasonCodeSchema, message: z.string() }).strict()), limits: mcpLimitsSchema, + unlimitedRates: z.literal(true).optional(), }).strict() export const mcpPolicyPreviewResponseSchema = z.object({ simulation: z.literal(true), @@ -136,6 +138,7 @@ export const mcpConnectionCreateSchema = z.object({ name: z.string().trim().min(1).max(100), authority: z.literal("service").optional(), allowUnattendedPlans: z.literal(true).optional(), + unlimitedRates: z.literal(true).optional(), expiresAt: instant.nullable(), mode: mcpModeSchema.default("observer"), permissions: uniqueStrings, @@ -146,7 +149,7 @@ export const mcpConnectionCreateSchema = z.object({ limits: mcpLimitsSchema.default(() => mcpLimitsSchema.parse({})), }).strict().refine(input => input.authority === "service" ? input.expiresAt === null && input.mode === "autonomous" && input.dangerousPolicy === "approval" && !input.autonomousDangerousOperationIds.length - : input.expiresAt !== null && !input.allowUnattendedPlans, "Опция доступна только бессрочному сервисному подключению. Для личного подключения укажите срок действия.") + : input.expiresAt !== null && !input.allowUnattendedPlans && !input.unlimitedRates, "Опция доступна только бессрочному сервисному подключению. Для личного подключения укажите срок действия.") .refine(input => !input.selectAllServers || !input.scope.serverIds.length, "Выберите все серверы или укажите конкретные ID") export const mcpConnectionSchema = z.object({ id: z.string().uuid(), name: z.string(), ownerId: z.string(), diff --git a/packages/contracts/src/traffic-flow.ts b/packages/contracts/src/traffic-flow.ts index 5a52b4c4..54597ef2 100644 --- a/packages/contracts/src/traffic-flow.ts +++ b/packages/contracts/src/traffic-flow.ts @@ -147,6 +147,7 @@ export const flowTalkerDtoSchema = z.object({ inIface: z.string(), inIfaceIndex: z.string().optional(), outIface: z.string().optional(), + accountedIface: z.string().optional(), nextHop: z.string().optional(), application: z.string().optional(), category: z.string().optional(), diff --git a/tests/e2e/mcp-auth-context.spec.ts b/tests/e2e/mcp-auth-context.spec.ts index 7af2443e..ab972960 100644 --- a/tests/e2e/mcp-auth-context.spec.ts +++ b/tests/e2e/mcp-auth-context.spec.ts @@ -389,7 +389,7 @@ test('synthetic: edit autonomous dangerous operations by group or all at once', }) })]) }) -for (const unattended of [false, true]) test(`synthetic: issue service access with unattended plans ${unattended ? 'enabled' : 'disabled'}`, async ({ page }) => { +for (const { unattended, unlimited } of [{ unattended: true, unlimited: false }, { unattended: false, unlimited: true }]) test(`synthetic: issue service access with ${unlimited ? 'unlimited rates' : 'unattended plans'}`, async ({ page }) => { const state = await setup(page, ['mm:mcp:admin', 'mm:servers:read']) await page.goto('/mcp?tab=connections') await page.getByRole('button', { name: 'Выдать доступ', exact: true }).click() @@ -405,6 +405,11 @@ for (const unattended of [false, true]) test(`synthetic: issue service access wi const unattendedChoice = dialog.getByRole('checkbox', { name: 'Выполнять планы без ручного подтверждения', exact: true }) await expect(unattendedChoice).not.toBeChecked() if (unattended) await unattendedChoice.check() + const rateChoice = dialog.getByRole('checkbox', { name: 'Отключить квоты частоты для этого ключа', exact: true }) + await expect(rateChoice).not.toBeChecked() + if (unlimited) await rateChoice.check() + await expect(dialog.getByLabel('Вызовов в минуту', { exact: true })).toHaveCount(unlimited ? 0 : 1) + await expect(dialog.getByLabel('Серверов на план', { exact: true })).toBeVisible() await next.click() await expect(dialog.getByText('Сервисное, бессрочное', { exact: false })).toBeVisible() await dialog.getByRole('button', { name: 'Сохранить доступ', exact: true }).click() @@ -414,6 +419,7 @@ for (const unattended of [false, true]) test(`synthetic: issue service access wi scope: { serverIds: [1], operationIds: ['logs.query'], allowGlobal: false }, }) })]) expect(state.mutations[0].body.allowUnattendedPlans).toBe(unattended ? true : undefined) + expect(state.mutations[0].body.unlimitedRates).toBe(unlimited ? true : undefined) }) test('synthetic: pause, resume, cancelled revoke, revoke, monitor denied outcome, stop automation', async ({ page }) => { diff --git a/tests/e2e/traffic-flow.spec.ts b/tests/e2e/traffic-flow.spec.ts index 56535f2b..3526f951 100644 --- a/tests/e2e/traffic-flow.spec.ts +++ b/tests/e2e/traffic-flow.spec.ts @@ -528,3 +528,26 @@ test('Flow: top-выборка и пустой локальный фильтр await page.getByRole('button', { name: 'сбросить', exact: true }).click() await expect(summary).toContainText('Показано 1 из 1 в выборке') }) + +test('Flow: сессии фильтруются по учтённому интерфейсу и точному пути', async ({ page }) => { + const control = await setup(page) + const session = { + serverId: '1', serverName: 'Экспортёр тест', src: '192.0.2.1', dst: '198.51.100.1', proto: 6, protoName: 'TCP', + srcPort: 1234, dstPort: 443, bytes: 100, packets: 1, bps: 100, inIface: 'lan', outIface: 'wan', + accountedIface: 'wan', clientId: 'u1', service: 'Тест', category: 'Веб', plane: 'payload', + } + control.metrics = { + conversations: 2, + conversationsList: [session, { ...session, outIface: 'wan2', accountedIface: 'wan2' }], + interfaces: [{ id: 'wan', label: 'wan', bytes: 100, packets: 1, bps: 100, percent: 50 }], + paths: [{ ...session, id: 'test-path', clientName: 'Клиент тест', enId: '', enName: '', ifaces: 'lan, wan' }], + } + await page.getByRole('button', { name: '15м', exact: true }).click() + const summary = page.getByRole('status', { name: 'Выборка сессий' }) + await page.getByRole('tab', { name: 'Интерфейсы', exact: true }).click() + await page.getByRole('row').filter({ hasText: 'wan' }).click() + await expect(summary).toContainText('Показано 1 из 2') + await page.getByRole('tab', { name: 'Пути', exact: true }).click() + await page.getByRole('row').filter({ hasText: 'Клиент тест' }).click() + await expect(summary).toContainText('Показано 1 из 2') +})