Compare commits

..
6 Commits
Author SHA1 Message Date
Denozordec 6f235c700a fix(mcp): учесть маршруты обмена трафиком в реестре
Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Successful in 10m35s
Docker images / backend-image (push) Successful in 3m10s
Docker images / frontend-image (push) Successful in 2m46s
Docker images / updater-image (push) Successful in 1m21s
Docker images / netflow-agent-image (push) Successful in 33s
Docker images / notify-webhook (push) Successful in 2s
Docker images / publish-release (push) Successful in 10s
2026-11-23 01:16:01 +07:00
Denozordec d422050a4b feat(ix): добавить точки обмена трафиком
Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Failing after 4m0s
Docker images / backend-image (push) Skipped
Docker images / frontend-image (push) Successful in 5m0s
Docker images / updater-image (push) Successful in 1m56s
Docker images / netflow-agent-image (push) Successful in 33s
Docker images / publish-release (push) Skipped
Docker images / notify-webhook (push) Skipped
2026-11-23 01:07:49 +07:00
Denozordec 80cd3a18d7 fix(network): исправить исходный адрес пинга и обновление фильтров
Docker images / prepare-release (push) Successful in 13s
Docker images / backend-test (push) Successful in 10m58s
Docker images / frontend-image (push) Successful in 3m56s
Docker images / updater-image (push) Successful in 2m37s
Docker images / netflow-agent-image (push) Successful in 39s
Docker images / backend-image (push) Successful in 3m56s
Docker images / publish-release (push) Successful in 12s
Docker images / notify-webhook (push) Successful in 2s
Оставить выбор исходного адреса пинга маршрутизатору. Читать правила и рекурсивные шлюзы последовательно, сохранить кэш при конфликте блокировки.
2026-11-22 20:47:38 +07:00
Denozordec f5914b634a fix(mcp): учесть модуль политики в тестах маршрутизации
Docker images / prepare-release (push) Successful in 12s
Docker images / backend-test (push) Successful in 10m46s
Docker images / backend-image (push) Successful in 3m6s
Docker images / frontend-image (push) Successful in 3m23s
Docker images / updater-image (push) Successful in 1m45s
Docker images / netflow-agent-image (push) Successful in 36s
Docker images / notify-webhook (push) Successful in 2s
Docker images / publish-release (push) Successful in 10s
2026-11-22 19:48:17 +07:00
Denozordec aac425c7b2 feat(mcp): расширить управление сетью и исправить разрезы трафика
Docker images / prepare-release (push) Successful in 13s
Docker images / backend-test (push) Failing after 10m39s
Docker images / backend-image (push) Skipped
Docker images / frontend-image (push) Successful in 3m11s
Docker images / updater-image (push) Successful in 1m56s
Docker images / netflow-agent-image (push) Successful in 37s
Docker images / publish-release (push) Skipped
Docker images / notify-webhook (push) Skipped
Добавить переименование интерфейсов и возможность отключать лимиты частоты для сервисных аккаунтов.

Уточнить дедупликацию, учёт интерфейсов и геоданных, фильтрацию сессий и карту потоков.
2026-11-22 19:24:49 +07:00
Denozordec d255694cda fix(mcp): исправить проверку зависимостей туннеля на больших таблицах маршрутов
Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Successful in 10m41s
Docker images / frontend-image (push) Successful in 2m13s
Docker images / updater-image (push) Successful in 1m17s
Docker images / netflow-agent-image (push) Successful in 33s
Docker images / backend-image (push) Successful in 1m58s
Docker images / publish-release (push) Successful in 11s
Docker images / notify-webhook (push) Successful in 2s
2026-11-22 17:51:05 +07:00
115 changed files with 3569 additions and 264 deletions

No files matched your search

+78 -33
View File
@@ -17,7 +17,7 @@ import { Separator } from "@/components/ui/separator"
import { cn } from "@/lib/utils"
import { filtersResponseSchema, filtersMutationSchema, filtersGreResponseSchema, filtersRecursiveResponseSchema } from "@/lib/filters-response"
import { firewallServersSchema } from "@/lib/firewall-response"
import { requestJson } from "@/shared/api/http-client"
import { ApiClientError, requestJson } from "@/shared/api/http-client"
import {
servers, greTunnels, serverFilterRulesets, filters as mockFiltersCatalog,
type FilterRule, type ServerFilterRuleset, type Server, type GreTunnel,
@@ -252,7 +252,7 @@ function generateCombinedRouterOSConfig(
// ── shared components ──────────────────────────────────────────────────────────
function TypeChip({ type }: { type: "jump-host" | "exit-node" | "home-router" }) {
function TypeChip({ type }: { type: import("@mmapp/contracts/servers").ServerType }) {
return (
<span className={cn(
"inline-flex items-center rounded px-1.5 py-0.5 text-[10px] font-bold border shrink-0",
@@ -261,7 +261,7 @@ function TypeChip({ type }: { type: "jump-host" | "exit-node" | "home-router" })
? "bg-violet-500/10 text-violet-600 dark:text-violet-400 border-violet-500/20"
: "bg-sky-500/10 text-sky-600 dark:text-sky-400 border-sky-500/20",
)}>
{type === "jump-host" ? "JH" : type === "home-router" ? "HR" : "EN"}
{type === "jump-host" ? "JH" : type === "home-router" ? "HR" : type === "ix-node" ? "IX" : "EN"}
</span>
)
}
@@ -874,7 +874,7 @@ function CopyRulesSheet({
greByServer: Record<string, GreTunnel[]>
ensureGreFor?: (serverId: string) => void
recRoutesByServer: Record<string, RecursiveRouteLite[]>
ensureRecursiveFor?: (serverId: string) => void
ensureRecursiveFor?: (serverId: string) => Promise<void>
catalogReady?: GatewayReadiness
}) {
const [selected, setSelected] = useState<Set<string>>(() => new Set(sourceRules.map(r => r.id)))
@@ -1229,7 +1229,11 @@ function CopyRulesSheet({
</fieldset>
{targetId && !mappingReady && <div className="px-5 py-3 flex flex-col gap-2">
<p role="alert" className="text-sm text-destructive">Каталоги или сопоставление шлюзов не подтверждены. Копирование маршрутов заблокировано.</p>
<Button variant="outline" disabled={copying} onClick={() => { for (const id of [sourceServerId, targetId]) { ensureGreFor?.(id); ensureRecursiveFor?.(id) } }}>Обновить шлюзы</Button>
<Button variant="outline" disabled={copying} onClick={() => {
ensureGreFor?.(sourceServerId)
ensureGreFor?.(targetId)
void ensureRecursiveFor?.(sourceServerId).then(() => ensureRecursiveFor(targetId))
}}>Обновить шлюзы</Button>
</div>}
{copyError && <p role="alert" className="px-5 py-3 text-sm text-destructive">{copyError}</p>}
{copying && <p role="status" className="px-5 py-2 text-sm text-muted-foreground">Ожидание подтверждения копирования…</p>}
@@ -1268,6 +1272,28 @@ function makeApiFetch(backendUrl: string) {
}
}
const configurationBusyMessage = "Конфигурация временно занята другой операцией"
function isConfigurationBusy(error: unknown): boolean {
if (!(error instanceof ApiClientError) || error.status !== 409) return false
const payload = error.payload
return (typeof payload === "object" && payload !== null && "code" in payload && payload.code === "CONFIGURATION_BUSY")
|| /^(?:Глобальная конфигурация|Конфигурация сервера) изменяется другой операцией$/.test(error.message)
}
async function readFiltersWithRetry<T>(read: () => Promise<T>, isCurrent: () => boolean): Promise<T> {
for (const delay of [350, 1000]) {
try {
return await read()
} catch (error) {
if (!isConfigurationBusy(error) || !isCurrent()) throw error
await new Promise(resolve => setTimeout(resolve, delay))
if (!isCurrent()) throw error
}
}
return read()
}
function FiltersPage() {
const { mode, backendUrl } = useDataSource()
const mounted = useRef(true)
@@ -1336,7 +1362,10 @@ function FiltersPage() {
setGatewayReady(prev => ({ ...prev, [serverId]: { ...prev[serverId], [kind]: false } }))
try {
const path = kind === "gre" ? "/api/filters/gre-tunnels" : "/api/recursive-routes"
const payload = await apiFetch<unknown>(`${path}?serverId=${encodeURIComponent(serverId)}`)
const payload = await readFiltersWithRetry(
() => apiFetch<unknown>(`${path}?serverId=${encodeURIComponent(serverId)}`),
() => mounted.current && gatewayEpoch.current[key] === epoch,
)
if (!mounted.current || gatewayEpoch.current[key] !== epoch) return
if (kind === "gre") {
const parsed = filtersGreResponseSchema.safeParse(payload)
@@ -1352,7 +1381,9 @@ function FiltersPage() {
setGatewayErrors(prev => ({ ...prev, [serverId]: { ...prev[serverId], [kind]: undefined } }))
} catch (error) {
if (!mounted.current || gatewayEpoch.current[key] !== epoch) return
setGatewayErrors(prev => ({ ...prev, [serverId]: { ...prev[serverId], [kind]: error instanceof Error ? error.message : "Каталог шлюзов недоступен" } }))
const message = isConfigurationBusy(error) ? configurationBusyMessage
: error instanceof Error ? error.message : "Каталог шлюзов недоступен"
setGatewayErrors(prev => ({ ...prev, [serverId]: { ...prev[serverId], [kind]: message } }))
}
}, [isLive, apiFetch])
const ensureGreTunnels = useCallback((serverId: string) => loadGatewayCatalog(serverId, "gre"), [loadGatewayCatalog])
@@ -1374,7 +1405,8 @@ function FiltersPage() {
const [movePending, setMovePending] = useState(false)
const [moveError, setMoveError] = useState<string | null>(null)
const moveLock = useRef(false)
const [liveStale, setLiveStale] = useState(false)
const [rulesReadFailure, setRulesReadFailure] = useState<"busy" | "unavailable" | null>(null)
const liveStale = rulesReadFailure !== null
const readEpoch = useRef(0)
const [readyServerId, setReadyServerId] = useState<string | null>(null)
const [rulesReading, setRulesReading] = useState(false)
@@ -1394,7 +1426,7 @@ function FiltersPage() {
setRulesets(buildRulesets(servers, serverFilterRulesets))
setSelectedServerId(servers[0]?.id ?? "")
setLiveLoadState("idle")
setLiveStale(false)
setRulesReadFailure(null)
})
return
}
@@ -1454,8 +1486,9 @@ function FiltersPage() {
setReadyServerId(null)
setRulesReading(true)
try {
const payload = await apiFetch<unknown>(
`/api/filters/rules?serverId=${encodeURIComponent(serverId)}`,
const payload = await readFiltersWithRetry(
() => apiFetch<unknown>(`/api/filters/rules?serverId=${encodeURIComponent(serverId)}`),
() => mounted.current && epoch === readEpoch.current,
)
if (epoch !== readEpoch.current) return false
const parsed = filtersResponseSchema.safeParse(payload)
@@ -1469,35 +1502,47 @@ function FiltersPage() {
if (!has) return [...prev, { serverId, rules: liveRules }]
return prev.map((rs) => rs.serverId === serverId ? { ...rs, rules: liveRules } : rs)
})
setLiveStale(false)
setRulesReadFailure(null)
setReadyServerId(serverId)
setRulesReceipt(prev => ({ ...prev, [serverId]: { at: new Date().toISOString(), kind: "read" } }))
return true
} catch (err) {
if (epoch !== readEpoch.current) return false
setLiveStale(true)
toast.error("Не удалось прочитать правила с роутера", { description: String(err) })
const busy = isConfigurationBusy(err)
setRulesReadFailure(busy ? "busy" : "unavailable")
if (!busy) toast.error("Не удалось прочитать правила с роутера", {
description: err instanceof Error ? err.message : "Ошибка чтения правил",
})
return false
} finally {
if (epoch === readEpoch.current) setRulesReading(false)
}
}, [isLive, apiFetch])
const loadServerFilters = useCallback(async (serverId: string) => {
const gre = ensureGreTunnels(serverId)
const rules = loadLiveRules(serverId)
const epoch = readEpoch.current
await rules
// Both these GETs record observations under the same exclusive application lock.
if (mounted.current && epoch === readEpoch.current) await ensureRecursiveRoutes(serverId)
await gre
}, [loadLiveRules, ensureGreTunnels, ensureRecursiveRoutes])
useEffect(() => {
if (!isLive || !selectedServerId || liveLoadState !== "idle") return
if (!liveServers.some((s) => s.id === selectedServerId)) return
void Promise.all([
loadLiveRules(selectedServerId),
ensureGreTunnels(selectedServerId),
ensureRecursiveRoutes(selectedServerId),
])
}, [isLive, selectedServerId, liveLoadState, liveServers, ensureGreTunnels, ensureRecursiveRoutes, loadLiveRules])
void loadServerFilters(selectedServerId)
}, [isLive, selectedServerId, liveLoadState, liveServers, loadServerFilters])
const allServers = !isLive ? servers : liveServers
const allTunnels = !isLive ? greTunnels : (greByServer[selectedServerId] ?? [])
const selectedServer = allServers.find(s => s.id === selectedServerId) ?? allServers[0]
const totalRules = rulesets.reduce((s, r) => s + r.rules.length, 0)
const mutationsLocked = (isLive && !can("mm:filters:write")) || movePending || (isLive && (applyBusy || historyRestoring || rulesReading || readyServerId !== selectedServerId || liveStale || liveLoadState !== "idle"))
const gatewayMessages = [...new Set(Object.values(gatewayErrors[selectedServerId] ?? {}))]
.filter((message): message is string => Boolean(message) && !(rulesReadFailure === "busy" && message === configurationBusyMessage))
const gatewaysBusy = gatewayMessages.every(message => message === configurationBusyMessage)
const filterRailItems = useMemo<ServerTileItem[]>(() => (
allServers.map((s) => ({
@@ -1518,7 +1563,7 @@ function FiltersPage() {
readEpoch.current += 1
setReadyServerId(null)
setRulesReading(false)
setLiveStale(false)
setRulesReadFailure(null)
setSheetOpen(false)
setPendingDelete(null)
setDeleteError(null)
@@ -1552,7 +1597,9 @@ function FiltersPage() {
if (!mounted.current) return { ok: false, error: "Источник данных изменён" }
if (applyLock.current || applyBusy || historyRestoring) return { ok: false, error: "Дождитесь завершения текущей операции" }
if (isLive && (liveStale || liveLoadState !== "idle" || (serverId === selectedServerId && readyServerId !== serverId))) {
const error = "Роутер недоступен — изменения заблокированы"
const error = rulesReadFailure === "busy"
? configurationBusyMessage + ". Изменения недоступны до обновления правил."
: "Роутер недоступен — изменения заблокированы"
toast.error(error)
return { ok: false, error }
}
@@ -1589,23 +1636,19 @@ function FiltersPage() {
applyLock.current = false
setApplyBusy(false)
}
}, [isLive, apiFetch, applyBusy, historyRestoring, liveStale, liveLoadState, readyServerId, selectedServerId])
}, [isLive, apiFetch, applyBusy, historyRestoring, liveStale, rulesReadFailure, liveLoadState, readyServerId, selectedServerId])
const refreshFromRouter = useCallback(async () => {
if (!isLive || !selectedServerId || !mounted.current || applyLock.current || applyBusy || historyRestoring) return
applyLock.current = true
setApplyBusy(true)
try {
await loadLiveRules(selectedServerId)
await Promise.all([
ensureGreTunnels(selectedServerId),
ensureRecursiveRoutes(selectedServerId),
])
await loadServerFilters(selectedServerId)
} finally {
applyLock.current = false
setApplyBusy(false)
}
}, [isLive, selectedServerId, applyBusy, historyRestoring, loadLiveRules, ensureGreTunnels, ensureRecursiveRoutes])
}, [isLive, selectedServerId, applyBusy, historyRestoring, loadServerFilters])
useEffect(() => () => { historyEpoch.current += 1; readEpoch.current += 1 }, [selectedServerId, backendUrl, isLive])
@@ -1653,7 +1696,7 @@ function FiltersPage() {
if (!await loadLiveRules(selectedServerId)) throw new Error("Версия применена, но актуальные правила не прочитаны — обновите данные")
}
if (!mounted.current) return
setLiveStale(false)
setRulesReadFailure(null)
setReadyServerId(selectedServerId)
await loadRevisions()
if (!mounted.current) return
@@ -1879,15 +1922,17 @@ function FiltersPage() {
<Button variant="outline" size="sm" onClick={() => setLoadAttempt(n => n + 1)}>Повторить загрузку</Button>
</div>
)}
{isLive && Object.values(gatewayErrors[selectedServerId] ?? {}).some(Boolean) && <div className="border-b px-6 py-3 flex items-center gap-3">
<p role="alert" className="text-xs text-destructive">{Object.values(gatewayErrors[selectedServerId] ?? {}).filter(Boolean).join(" · ")}. Сохранены последние каталоги шлюзов.</p>
{isLive && gatewayMessages.length > 0 && <div className="border-b px-6 py-3 flex items-center gap-3">
<p role={gatewaysBusy ? "status" : "alert"} className={cn("text-xs", gatewaysBusy ? "text-muted-foreground" : "text-destructive")}>{gatewayMessages.join(" · ")}. Сохранены последние каталоги шлюзов.</p>
<Button variant="outline" size="sm" disabled={applyBusy} onClick={() => { void ensureGreTunnels(selectedServerId); void ensureRecursiveRoutes(selectedServerId) }}>Обновить шлюзы</Button>
</div>}
{isLive && rulesReading && <p role="status" className="border-b px-6 py-2 text-xs text-muted-foreground">Чтение актуальных правил устройства… Изменения временно заблокированы.</p>}
{isLive && liveStale && liveLoadState !== "error" && (
<div className="shrink-0 border-b border-amber-500/30 bg-amber-500/10 px-6 py-2.5 text-xs text-amber-700 dark:text-amber-400 flex items-center gap-2">
<AlertTriangleIcon className="size-3.5 shrink-0" />
Роутер недоступен — показан кэш. Изменения заблокированы, пока не удастся прочитать CHR.
{rulesReadFailure === "busy"
? configurationBusyMessage + ". Показаны последние правила. Изменения недоступны до обновления правил."
: "Роутер недоступен — показан кэш. Изменения заблокированы, пока не удастся прочитать CHR."}
</div>
)}
<div className="border-b px-4 py-3 flex items-center gap-3 flex-wrap shrink-0 md:px-6">
+2 -1
View File
@@ -1,5 +1,6 @@
"use client"
import { serverTypeSchema } from "@mmapp/contracts/servers"
import { can } from "@/lib/auth"
import { z } from "zod"
@@ -174,7 +175,7 @@ function mapBackendServer(s: BackendServer): InventoryServer {
const ipsecInventorySchema = z.array(z.object({
id: z.number().int().positive(), name: z.string(), host: z.string(), enabled: z.boolean(),
country: z.string().optional().default("UN"),
type: z.enum(["jump-host", "exit-node", "home-router"]).optional(),
type: serverTypeSchema.optional(),
status: z.enum(["online", "offline", "degraded"]).nullable().optional(),
latency: z.number().nullable().optional(), asn: z.string().optional(),
})).refine(rows => new Set(rows.map(row => row.id)).size === rows.length)
+250
View File
@@ -0,0 +1,250 @@
"use client"
import { useEffect, useMemo, useState } from "react"
import Link from "next/link"
import { NetworkIcon, PlusIcon, RefreshCwIcon } from "lucide-react"
import { type ColumnDef, useTable } from "@tanstack/react-table"
import { ixSegmentConfigurationSchema, type IxChange, type IxRouterObject, type IxSegment, type IxSiteObservation } from "@mmapp/contracts/internet-exchange"
import { Badge } from "@/components/reui/badge"
import { dataGridFeatures, type DataGridFeatures } from "@/components/reui/data-grid"
import { DataGridShell } from "@/components/data-grids/shared/data-grid-shell"
import { EmptyState } from "@/components/empty-state"
import { PageHeader } from "@/components/page-header"
import { Button, buttonVariants } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Textarea } from "@/components/ui/textarea"
import { can, ensureAuthConfig } from "@/lib/auth"
import { useDataSource } from "@/lib/data-source"
import { requestJson } from "@/shared/api/http-client"
import { mcpApi } from "@/shared/api/mcp"
type Node = { id: number; name: string; site: string; country: string; enabled: boolean }
type Connection = { id: string; name: string }
type Plan = { planId: string; state: string }
const describe = (error: unknown) => error instanceof Error ? error.message : "Не удалось выполнить операцию IX"
const json = (value: unknown) => JSON.stringify(value, null, 2)
const post = (value: unknown) => ({ method: "POST", body: JSON.stringify(value) })
function SegmentGrid({ rows, open }: { rows: IxSegment[]; open: (segment: IxSegment) => void }) {
const columns: ColumnDef<DataGridFeatures, IxSegment>[] = [
{ id: "name", header: "Сегмент", cell: ({ row }) => <Button variant="ghost" size="sm" onClick={() => open(row.original)}>{row.original.configuration.name}</Button> },
{ id: "sites", header: "Площадки", cell: ({ row }) => row.original.configuration.sites.length },
{ id: "neighbors", header: "BGP-соседи", cell: ({ row }) => row.original.configuration.sites.reduce((total, site) => total + site.neighbors.length, 0) },
{ id: "state", header: "Состояние конфигурации", cell: ({ row }) => row.original.revision === row.original.appliedRevision
? <Badge variant="success-light">Применена</Badge>
: <Badge variant="warning-light">Черновик</Badge> },
]
const table = useTable({ features: dataGridFeatures, manualPagination: true, data: rows, columns, getRowId: row => row.id })
return <DataGridShell table={table} recordCount={rows.length} />
}
export default function IxPage() {
const { backendUrl, mode, prefsHydrated } = useDataSource()
const [segments, setSegments] = useState<IxSegment[]>([])
const [nodes, setNodes] = useState<Node[]>([])
const [connections, setConnections] = useState<Connection[]>([])
const [connectionId, setConnectionId] = useState("")
const [selected, setSelected] = useState<IxSegment | null>(null)
const [editing, setEditing] = useState(false)
const [body, setBody] = useState("")
const [filter, setFilter] = useState("")
const [nodeId, setNodeId] = useState("")
const [name, setName] = useState("")
const [asn, setAsn] = useState("64512")
const [routerId, setRouterId] = useState("")
const [observations, setObservations] = useState<IxSiteObservation[]>([])
const [preview, setPreview] = useState<IxRouterObject[] | null>(null)
const [linux, setLinux] = useState("")
const [plan, setPlan] = useState<Plan | null>(null)
const [tab, setTab] = useState<"configuration" | "routes" | "changes">("configuration")
const [busy, setBusy] = useState(false)
const [error, setError] = useState("")
const [write, setWrite] = useState(false)
const [approve, setApprove] = useState(false)
async function load(signal?: AbortSignal) {
const options = { signal }
const [segmentsResult, nodesResult] = await Promise.all([
requestJson<{ items: IxSegment[] }>(backendUrl, "/api/ix/segments?limit=100", options),
requestJson<{ items: Node[] }>(backendUrl, "/api/ix/nodes", options),
])
setSegments(segmentsResult.items)
setNodes(nodesResult.items)
if (can("mm:mcp:read")) {
const { connections } = await mcpApi(backendUrl).connections(signal)
setConnections(connections.filter(connection => connection.policy.status === "active" &&
connection.policy.scope.operationIds.includes("network.ix.configure")).map(connection => ({ id: connection.id, name: connection.name })))
}
}
useEffect(() => {
if (!prefsHydrated || mode !== "live") return
const controller = new AbortController()
void ensureAuthConfig().then(async () => {
if (controller.signal.aborted) return
setWrite(can("mm:network:write"))
setApprove(can("mm:mcp:approve"))
await load(controller.signal)
}).catch(error => { if (!controller.signal.aborted) setError(describe(error)) })
return () => controller.abort()
// Load only when the project source changes.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [backendUrl, mode, prefsHydrated])
const rows = useMemo(() => segments.filter(segment => segment.configuration.name.includes(filter.toLowerCase().trim())), [filter, segments])
function open(segment: IxSegment | null) {
setSelected(segment)
setBody(segment ? json(segment.configuration) : "")
setPreview(null)
setPlan(null)
setObservations([])
setLinux("")
setTab("configuration")
setError("")
setEditing(true)
}
function change(): IxChange {
const configuration = ixSegmentConfigurationSchema.parse(JSON.parse(body))
return { ...(selected ? { id: selected.id } : {}), revision: selected?.revision ?? 0, configuration }
}
async function perform(work: () => Promise<void>) {
setBusy(true)
setError("")
try { await work() } catch (error) { setError(describe(error)) }
finally { setBusy(false) }
}
async function save() {
const { segment } = await requestJson<{ segment: IxSegment }>(backendUrl, "/api/ix/segments", post(change()))
open(segment)
await load()
}
async function observe() {
if (!selected) return
const { sites } = await requestJson<{ sites: IxSiteObservation[] }>(backendUrl, `/api/ix/segments/${selected.id}/status`)
setObservations(sites)
setTab("routes")
}
return <>
<PageHeader crumbs={[{ label: "Сеть" }, { label: "Точки обмена трафиком" }]} />
<div className="mx-auto flex w-full max-w-6xl flex-1 flex-col gap-6 p-4 md:p-6">
<div className="flex flex-wrap items-start justify-between gap-3">
<div>
<h1 className="text-2xl font-semibold tracking-tight">Точки обмена трафиком</h1>
<p className="mt-1 text-sm text-muted-foreground">IX-сегменты, маршруты и BGP-политики в отдельных VRF.</p>
</div>
<div className="flex gap-2">
<Button variant="outline" size="icon" disabled={busy || mode !== "live"} aria-label="Обновить список" onClick={() => void perform(() => load())}><RefreshCwIcon /></Button>
{write && <Button onClick={() => open(null)} disabled={busy}><PlusIcon />Добавить сегмент</Button>}
</div>
</div>
{error && <p role="alert" className="rounded-md border border-destructive/30 bg-destructive/5 p-3 text-sm text-destructive">{error}</p>}
<div className="grid grid-cols-1 gap-3 sm:grid-cols-3">
{[{ name: "Узлы IX", count: nodes.length }, { name: "Сегменты", count: segments.length }, { name: "Применённые ревизии", count: segments.filter(segment => segment.appliedRevision === segment.revision).length }].map(item =>
<div key={item.name} className="rounded-lg border bg-card p-4">
<div className="text-sm text-muted-foreground">{item.name}</div>
<div className="mt-1 text-2xl font-semibold tabular-nums">{item.count}</div>
</div>
)}
</div>
<section className="flex flex-col gap-3" aria-label="IX-сегменты">
<Input aria-label="Поиск сегментов" placeholder="Найти сегмент" className="max-w-xs" value={filter} onChange={event => setFilter(event.target.value)} />
{rows.length ? <SegmentGrid rows={rows} open={open} /> :
<EmptyState icon={<NetworkIcon className="size-4" />} title={filter ? "Сегменты не найдены" : "Нет IX-сегментов"}
description={mode !== "live" ? "Сегменты доступны при подключении к серверу." : "Добавьте сервер типа IX Node, чтобы создать сегмент."} />}
</section>
{editing && <section className="flex flex-col gap-4 rounded-lg border bg-card p-4" aria-label="Карточка IX">
<div className="flex flex-wrap items-center justify-between gap-2">
<h2 className="text-lg font-semibold">{selected?.configuration.name ?? "Новый сегмент"}</h2>
<Button variant="ghost" size="sm" onClick={() => setEditing(false)}>Закрыть</Button>
</div>
<div className="flex flex-wrap gap-2">
<Button size="sm" variant={tab === "configuration" ? "secondary" : "ghost"} onClick={() => setTab("configuration")}>Конфигурация</Button>
<Button size="sm" variant={tab === "routes" ? "secondary" : "ghost"} disabled={!selected?.appliedRevision || busy} onClick={() => void perform(observe)}>Маршруты и BGP</Button>
<Button size="sm" variant={tab === "changes" ? "secondary" : "ghost"} disabled={!preview && !plan} onClick={() => setTab("changes")}>Изменения</Button>
</div>
{tab === "configuration" && <>
{!body && <div className="grid gap-3 sm:grid-cols-2">
<div className="grid gap-2"><Label htmlFor="ix-name">Имя сегмента</Label><Input id="ix-name" placeholder="compute" value={name} onChange={event => setName(event.target.value)} /></div>
<div className="grid gap-2"><Label htmlFor="ix-site">Площадка</Label><select id="ix-site" className="h-9 rounded-md border bg-background px-3 text-sm" value={nodeId} onChange={event => setNodeId(event.target.value)}>
<option value="">Выберите IX Node</option>
{nodes.filter(node => node.enabled).map(node => <option key={node.id} value={node.id}>{node.name || node.id}</option>)}
</select></div>
<div className="grid gap-2"><Label htmlFor="ix-asn">Локальный ASN</Label><Input id="ix-asn" inputMode="numeric" value={asn} onChange={event => setAsn(event.target.value)} /></div>
<div className="grid gap-2"><Label htmlFor="ix-router-id">Router ID</Label><Input id="ix-router-id" placeholder="192.0.2.10" value={routerId} onChange={event => setRouterId(event.target.value)} /></div>
<Button variant="outline" disabled={!nodeId || !name || !routerId} onClick={() => void perform(async () => {
setBody(json(ixSegmentConfigurationSchema.parse({ name, sites: [{ serverId: Number(nodeId), autonomousSystem: Number(asn), routerId }] })))
})}>Подготовить конфигурацию</Button>
<Link className={buttonVariants({ variant: "link" })} href="/servers">Открыть каталог серверов</Link>
</div>}
{body && <div className="grid gap-2">
<Label htmlFor="ix-configuration">Конфигурация сегмента</Label>
<Textarea id="ix-configuration" spellCheck={false} readOnly={!write} rows={13} className="font-mono" value={body} onChange={event => {
setBody(event.target.value); setPlan(null); setPreview(null)
}} />
</div>}
{body && write && <div className="flex flex-wrap items-end gap-2">
<Button onClick={() => void perform(save)} disabled={busy}>Сохранить черновик</Button>
<Button variant="outline" disabled={busy} onClick={() => void perform(async () => {
const result = await requestJson<{ objects: IxRouterObject[] }>(backendUrl, "/api/ix/preview", post(change()))
setPreview(result.objects); setTab("changes")
})}>Предпросмотр</Button>
<div className="grid gap-2"><Label htmlFor="ix-connection">Подключение MCP</Label><select id="ix-connection" className="h-9 rounded-md border bg-background px-3 text-sm" value={connectionId} onChange={event => setConnectionId(event.target.value)}>
<option value="">Выберите подключение</option>
{connections.map(connection => <option key={connection.id} value={connection.id}>{connection.name}</option>)}
</select></div>
<Button variant="outline" disabled={!connectionId || busy} onClick={() => void perform(async () => {
setPlan(await requestJson<Plan>(backendUrl, "/api/mcp/ix/plan", post({ connectionId, change: change() })))
setTab("changes")
})}>Подготовить план MCP</Button>
</div>}
</>}
{tab === "changes" && <>
{preview && <div className="grid gap-2"><p className="text-sm">{preview.length} объектов в желаемой конфигурации</p>
<pre className="max-h-96 overflow-auto rounded-md border bg-muted/30 p-3 text-sm">{json(preview)}</pre></div>}
{plan && <div className="flex flex-wrap items-center gap-2">
<Badge variant="info-light">{plan.state}</Badge>
<span className="text-sm text-muted-foreground">{plan.planId}</span>
<Link className={buttonVariants({ variant: "link" })} href="/mcp">Планы и операции</Link>
{plan.state === "awaiting-approval" && approve && <Button variant="outline" disabled={busy} onClick={() => void perform(async () => {
await mcpApi(backendUrl).decision(plan.planId, true)
setPlan({ ...plan, state: "approved" })
})}>Подтвердить</Button>}
{["draft", "approved"].includes(plan.state) && <Button disabled={busy} onClick={() => void perform(async () => {
const result = await mcpApi(backendUrl).apply(plan.planId)
setPlan({ ...plan, state: result.state })
})}>Применить план</Button>}
</div>}
</>}
{tab === "routes" && <div className="grid gap-4">
{selected?.configuration.sites.flatMap(site => site.interfaces.filter(binding => binding.provisionWireguard?.hubEndpoint).map(binding =>
<Button key={site.serverId + ":" + binding.name} size="sm" variant="outline" className="w-fit" disabled={busy} onClick={() => void perform(async () => {
const query = new URLSearchParams({ serverId: String(site.serverId), interfaceName: binding.name })
const result = await requestJson<{ template: string }>(backendUrl, `/api/ix/segments/${selected.id}/linux?${query}`)
setLinux(result.template)
})}>Конфигурация Linux · {binding.name}</Button>
))}
{linux && <pre className="overflow-auto rounded-md border bg-muted/30 p-3 text-sm">{linux}</pre>}
{observations.map(site => <div key={site.serverId} className="grid gap-2">
<div className="flex flex-wrap justify-between gap-2 text-sm">
<span className="font-medium">{nodes.find(node => node.id === site.serverId)?.name || site.serverId}</span>
<span className="text-muted-foreground">{new Date(site.observedAt).toLocaleString()}</span>
</div>
{site.available ? <>
<p className="text-sm text-muted-foreground">Активные BGP-сессии {site.neighbors.filter(peer => peer.established).length} из {site.neighbors.length}</p>
<div className="overflow-x-auto"><table className="w-full text-left text-sm">
<thead><tr className="border-b"><th className="p-2">Префикс</th><th className="p-2">Шлюз</th><th className="p-2">Фактический next hop</th><th className="p-2">Активен</th></tr></thead>
<tbody>{site.routes.map((route, index) => <tr key={route.prefix + ":" + index} className="border-b">
<td className="p-2 font-mono">{route.prefix}</td><td className="p-2">{route.gateway || "—"}</td>
<td className="p-2">{route.immediateGateway ?? "Не подтверждён"}</td><td className="p-2">{route.active ? "Да" : "Нет"}</td>
</tr>)}</tbody>
</table></div>
</> : <Badge variant="warning-light">Состояние недоступно</Badge>}
</div>)}
</div>}
</section>}
</div>
</>
}
+27
View File
@@ -243,6 +243,7 @@ const TYPE_STYLE: Record<ServerType, { label: string; fill: string; r: number }>
"jump-host": { label: "JH", fill: "var(--chart-1)", r: 34 },
// EN — крайние edge-узлы, менее акцентные.
"exit-node": { label: "EN", fill: "var(--chart-3)", r: 30 },
"ix-node": { label: "IX", fill: "var(--chart-4)", r: 34 },
}
const WAN_COLORS = ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)", "var(--chart-4)", "var(--chart-5)"]
@@ -257,6 +258,7 @@ const TYPE_LABELS: Record<ServerType, string> = {
"jump-host": "JumpHost",
"exit-node": "Exit Node",
"home-router": "Home Router",
"ix-node": "IX Node",
}
const MOCK_MAP_SERVICES: FlowMapService[] = [
@@ -1153,6 +1155,17 @@ function NetworkMapContent() {
const [mapServers, setMapServers] = useState<Server[]>([])
const [sessionCounts,setSessionCounts]=useState<Record<string,number|null>>({})
const [mapGreTunnels, setMapGreTunnels] = useState<GreTunnel[]>([])
const [showIx, setShowIx] = useState(true)
const [ixLinks, setIxLinks] = useState<Array<{ from: number; to: number; segment: string }>>([])
const hasIxNodes = mapServers.some(server => server.type === "ix-node")
useEffect(() => {
if (!useLiveData || !hasIxNodes) { setIxLinks([]); return }
const controller = new AbortController()
void apiFetch<{ links: Array<{ from: number; to: number; segment: string }> }>("/api/ix/topology", { signal: controller.signal })
.then(result => { if (!controller.signal.aborted) setIxLinks(result.links) })
.catch(() => { if (!controller.signal.aborted) setIxLinks([]) })
return () => controller.abort()
}, [apiFetch, useLiveData, hasIxNodes])
const [speedProbes, setSpeedProbes] = useState<GreSpeedProbeSnapshot[]>([])
const [mapHops, setMapHops] = useState<FlowMapHop[]>([])
const [mapServices, setMapServices] = useState<FlowMapService[]>([])
@@ -2260,6 +2273,10 @@ function NetworkMapContent() {
{/* ── Canvas ── */}
<div ref={canvasRef} className={cn("flex-1 min-h-0 min-w-0 relative overflow-hidden bg-background select-none", (selected || selectedGreEdge || selectedService) && "hidden lg:block")}>
<NetworkMapLegend />
{ixLinks.length > 0 && <button type="button" aria-pressed={showIx} onClick={() => setShowIx(value => !value)}
className="absolute right-3 top-3 z-20 rounded-md border bg-popover px-3 py-2 text-xs font-medium">
Связи IX
</button>}
<p id="network-map-help" className="sr-only">Стрелки перемещают карту, плюс и минус меняют масштаб, F вписывает объекты. Tab выбирает объект, Enter открывает детали.</p>
<svg
ref={svgRef} role="group" aria-label="Интерактивная карта сети" tabIndex={0} aria-describedby="network-map-help"
@@ -2395,6 +2412,16 @@ function NetworkMapContent() {
)
})}
{showIx && [...new Map(ixLinks.map(link => {
const key = [Math.min(link.from, link.to), Math.max(link.from, link.to), link.segment].join(":")
return [key, link] as const
})).entries()].map(([key, link]) => {
const from = nodePosById[String(link.from)], to = nodePosById[String(link.to)]
return from && to && <line key={key} x1={from.x} y1={from.y} x2={to.x} y2={to.y}
stroke="var(--chart-4)" strokeWidth={2} strokeDasharray="8 4">
<title>IX-сегмент {link.segment} · настроенная связь</title>
</line>
})}
{/* ── Home Router → WAN satellite connectors ── */}
{homeRouters.flatMap(router => {
const rNode = nodes.find(n => n.id === router.id)
+3 -3
View File
@@ -35,7 +35,7 @@ interface BackendServer {
id: number
name: string
host: string
type?: "jump-host" | "exit-node" | "home-router"
type?: import("@mmapp/contracts/servers").ServerType
site: string
country: string
asn: string
@@ -329,11 +329,11 @@ function RouteSheet({
)
}
function TypeChip({ type }: { type: "jump-host" | "exit-node" | "home-router" }) {
function TypeChip({ type }: { type: import("@mmapp/contracts/servers").ServerType }) {
return (
<Badge size="xs" className="shrink-0 text-foreground"
variant={type === "home-router" ? "success-light" : type === "jump-host" ? "primary-light" : "info-light"}>
{type === "jump-host" ? "JH" : type === "home-router" ? "HR" : "EN"}
{type === "jump-host" ? "JH" : type === "home-router" ? "HR" : type === "ix-node" ? "IX" : "EN"}
</Badge>
)
}
+3
View File
@@ -591,6 +591,7 @@ function ServersPageContent() {
"jump-host": serverList.filter(s => s.type === "jump-host").length,
"exit-node": serverList.filter(s => s.type === "exit-node").length,
"home-router":serverList.filter(s => s.type === "home-router").length,
"ix-node": serverList.filter(s => s.type === "ix-node").length,
online: serverList.filter(s => s.status === "online").length,
}), [serverList])
@@ -599,6 +600,7 @@ function ServersPageContent() {
{ value: "jump-host", label: "JumpHost" },
{ value: "exit-node", label: "Exit Node" },
{ value: "home-router", label: "Home Router" },
{ value: "ix-node", label: "IX Node" },
]
const isHomeRouter = form.type === "home-router"
@@ -779,6 +781,7 @@ function ServersPageContent() {
{ value: "home-router", label: "Home Router" },
{ value: "jump-host", label: "JumpHost" },
{ value: "exit-node", label: "Exit Node" },
{ value: "ix-node", label: "IX Node" },
]}
/>
</FormField>
+5 -4
View File
@@ -235,7 +235,7 @@ function probeGroupActionKey(srvId: string, group: { name: string; target: strin
// ── shared components ──────────────────────────────────────────────────────────
function TypeChip({ type }: { type: "jump-host" | "exit-node" | "home-router" }) {
function TypeChip({ type }: { type: import("@mmapp/contracts/servers").ServerType }) {
return (
<span className={cn(
"inline-flex items-center rounded px-1.5 py-0.5 text-[10px] font-bold border shrink-0",
@@ -245,7 +245,7 @@ function TypeChip({ type }: { type: "jump-host" | "exit-node" | "home-router" })
? "bg-violet-500/10 text-violet-600 dark:text-violet-400 border-violet-500/20"
: "bg-sky-500/10 text-sky-600 dark:text-sky-400 border-sky-500/20",
)}>
{type === "jump-host" ? "JH" : type === "home-router" ? "HR" : "EN"}
{type === "jump-host" ? "JH" : type === "home-router" ? "HR" : type === "ix-node" ? "IX" : "EN"}
</span>
)
}
@@ -666,7 +666,7 @@ interface BackendServer {
id: number
name: string
host: string
type: "jump-host" | "exit-node" | "home-router"
type: import("@mmapp/contracts/servers").ServerType
site: string
country: string
enabled: boolean
@@ -843,7 +843,7 @@ const INIT_RESOURCES: ServerResource[] = mockServers.map(s => {
// ── ResourcesTab ──────────────────────────────────────────────────────────────
type ResTypeFilter = "all" | "jump-host" | "exit-node" | "home-router"
type ResTypeFilter = "all" | import("@mmapp/contracts/servers").ServerType
function ResourceWarnings({ rows }: { rows: UptimeResourceRow[] }) {
const [expanded, setExpanded] = useState(false)
@@ -941,6 +941,7 @@ function ResourcesTab({ resources, serversList, liveApi }: { resources: ServerRe
{ value: "jump-host", label: "JumpHost" },
{ value: "exit-node", label: "Exit Node" },
{ value: "home-router", label: "Home Router" },
{ value: "ix-node", label: "IX Node" },
]
return (
@@ -0,0 +1,34 @@
BEGIN;
ALTER TABLE public.servers DROP CONSTRAINT servers_type_check;
ALTER TABLE public.servers ADD CONSTRAINT servers_type_check
CHECK (type IN ('jump-host', 'exit-node', 'home-router', 'ix-node'));
CREATE TABLE public.ix_segments (
id uuid PRIMARY KEY,
name text NOT NULL UNIQUE,
revision integer NOT NULL CHECK (revision > 0),
configuration jsonb NOT NULL CHECK (jsonb_typeof(configuration) = 'object'),
applied_revision integer CHECK (applied_revision > 0 AND applied_revision <= revision),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE public.ix_segment_devices (
segment_id uuid NOT NULL REFERENCES public.ix_segments(id) ON DELETE CASCADE,
server_id bigint NOT NULL REFERENCES public.servers(id) ON DELETE RESTRICT,
PRIMARY KEY (segment_id, server_id)
);
CREATE INDEX ix_segment_devices_server_idx ON public.ix_segment_devices(server_id);
CREATE TABLE public.ix_segment_revisions (
segment_id uuid NOT NULL REFERENCES public.ix_segments(id) ON DELETE CASCADE,
revision integer NOT NULL CHECK (revision > 0),
configuration jsonb NOT NULL CHECK (jsonb_typeof(configuration) = 'object'),
actor_id text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (segment_id, revision)
);
INSERT INTO public.schema_migrations (id) VALUES ('0058_internet_exchange') ON CONFLICT (id) DO NOTHING;
COMMIT;
+63
View File
@@ -464,6 +464,41 @@
],
"reason": "Общий listRevisions с REST: только метаданные, максимум 50 записей; серверная область и права проверяются до SQL, payload ревизии не выдаётся."
},
{
"method": "GET",
"path": "/api/ix/nodes",
"status": "excluded",
"operations": [],
"reason": "Служебная проекция для выбора площадок в интерфейсе. В MCP доступ к серверам определяется областью подключения."
},
{
"method": "GET",
"path": "/api/ix/segments",
"status": "mediated",
"operations": ["network.ix.list"],
"reason": "Общий сервис списка сегментов. MCP ограничивает выборку областью серверов и возвращает метаданные ревизий."
},
{
"method": "GET",
"path": "/api/ix/segments/:id/linux",
"status": "excluded",
"operations": [],
"reason": "Шаблон подключения Linux оператор получает в интерфейсе для локальной настройки клиента."
},
{
"method": "GET",
"path": "/api/ix/segments/:id/status",
"status": "mediated",
"operations": ["network.ix.read"],
"reason": "Общий опрос маршрутов и BGP-сессий применённой ревизии. MCP проверяет доступ ко всем узлам сегмента."
},
{
"method": "GET",
"path": "/api/ix/topology",
"status": "excluded",
"operations": [],
"reason": "Проекция применённых связей для сетевой карты. MCP читает конфигурацию и состояние отдельных сегментов."
},
{
"method": "GET",
"path": "/api/logs",
@@ -1562,6 +1597,20 @@
],
"reason": "Общий REST/MCP executor существующего сертификата: оба selector и все роли, явный private/CA intent, human MCP plan, actor/key no-replay, exact owned file/read/cleanup, encrypted owner-only artifact/TTL; синтетическая SQL/HTTP/portal и browser приёмка. Native CHR отдельно."
},
{
"method": "POST",
"path": "/api/ix/preview",
"status": "excluded",
"operations": [],
"reason": "Локальный предпросмотр объектов из компилятора. План MCP дополнительно фиксирует состояние площадок и требует подтверждения."
},
{
"method": "POST",
"path": "/api/ix/segments",
"status": "excluded",
"operations": [],
"reason": "Сохранение черновика из интерфейса. Для изменения конфигурации через MCP используется план с подтверждением."
},
{
"method": "POST",
"path": "/api/mcp/connections",
@@ -1632,6 +1681,20 @@
"operations": ["network.bridge-vlan.configure"],
"reason": "Подготовка связанного плана bridge/VLAN через тот же MCP-инструмент; выполнение требует человеческого подтверждения."
},
{
"method": "POST",
"path": "/api/mcp/ix/plan",
"status": "mediated",
"operations": ["network.ix.configure"],
"reason": "Интерфейс владельца вызывает тот же инструмент MCP подготовки плана IX. Применение проходит отдельно после подтверждения."
},
{
"method": "POST",
"path": "/api/mcp/ix/restore",
"status": "mediated",
"operations": ["network.ix.restore"],
"reason": "Интерфейс владельца вызывает тот же инструмент MCP подготовки восстановления сохранённой ревизии IX."
},
{
"method": "POST",
"path": "/api/mcp/management-channels",
+36
View File
@@ -0,0 +1,36 @@
import { execFileSync } from "node:child_process"
import { homedir } from "node:os"
import { join } from "node:path"
export const hosts = ["194.226.121.150", "104.165.244.7"] as const
const key = join(homedir(), ".ssh", "chr_claude")
const known = process.env.MMAPP_IX_TEST_KNOWN_HOSTS ?? join(homedir(), ".ssh", "known_hosts")
export const quote = (value: string) => '"' + value.replace(/[\\"$]/g, "\\$&").replace(/\n/g, "\\n") + '"'
export const sshOptions = ["-F", process.platform === "win32" ? "NUL" : "/dev/null", "-i", key,
"-o", "IdentitiesOnly=yes", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=yes", "-o", "UserKnownHostsFile=" + known,
"-o", "ConnectTimeout=12", "-o", "LogLevel=ERROR"]
export function cli(side: number, expression: string, stage?: string) {
const args = [...sshOptions, "claude@" + hosts[side],
`:onerror err in={ ${expression.replace(/\r?\n\s*/g, " ")}; :put "IX_OK" } do={ :put ("IX_ERROR:" . $err) }`]
const run = () => execFileSync("ssh", args, { encoding: "utf8", timeout: 35000, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] }).trim()
let stdout: string
try {
for (let attempt = 0;; attempt++) {
try { stdout = run(); break } catch (error) {
// Retry only when the command could not have reached the router.
const failure = error as { status?: number; stderr?: string; stdout?: string }
if (attempt >= 2 || failure.status !== 255 || failure.stdout || !/^ssh: connect to host .* port 22: Connection timed out\s*$/i.test(failure.stderr ?? "")) throw error
}
}
} catch (error) {
if (stage) throw Error(`CHR ${side + 1}: SSH failed during ${stage}`)
throw error
}
if (!stdout.split(/\r?\n/).includes("IX_OK") || stdout.includes("IX_ERROR") || /syntax error|expected end|expected closing/.test(stdout)) {
const diagnostic = stdout.replace(/[a-f0-9]{36,}/gi, "[redacted]").slice(0, 420)
throw Error(`CHR ${side + 1} rejected ${stage ?? expression.slice(0, 360)} · ${diagnostic}`)
}
return stdout.replace(/\r?\n?IX_OK$/, "").trim()
}
+264
View File
@@ -0,0 +1,264 @@
import assert from "node:assert/strict"
import { randomBytes, randomUUID, X509Certificate } from "node:crypto"
import { mkdtemp } from "node:fs/promises"
import { createServer, isIPv4 } from "node:net"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { setTimeout as delay } from "node:timers/promises"
import { getCACertificates, setDefaultCACertificates } from "node:tls"
import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client"
import EmbeddedPostgres from "embedded-postgres"
import { SignJWT } from "jose"
import { ixNamespace } from "../../src/services/internet-exchange-plan.js"
import { cli, hosts, quote } from "./ix-chr-ssh.mjs"
assert.deepEqual(process.argv.slice(2), ["--owned-test-pair"])
for (const name of Object.keys(process.env)) {
if (/^(DATABASE_|MCP_|AUTH_|PG|MMAPP_)/.test(name)) delete process.env[name]
}
process.env.DOTENV_CONFIG_PATH = join(tmpdir(), "mmapp-ix-no-env-" + randomUUID())
const id = randomUUID(), ns = ixNamespace(id), short = ns.slice(0, 12)
const segment = short + "-gre", gre = short + "-gre", pin = (side: number) => "10.245.0." + (1 + side)
const psk = randomBytes(32).toString("hex"), password = randomBytes(24).toString("hex")
const roots = getCACertificates("default")
const certificates: string[] = []
const entered = new Map<number, string>()
const result: Record<string, unknown> = {}
let appliedNamespace: string | undefined
const ipsecPaths = ["/ip/ipsec/policy", "/ip/ipsec/identity", "/ip/ipsec/peer", "/ip/ipsec/proposal", "/ip/ipsec/profile"]
const ixPaths = ["/ip/route", "/ip/address", "/ip/firewall/filter", "/ipv6/firewall/filter", "/ip/vrf", "/interface/bridge"]
const sides = [0, 1]
function baseline(side: number) {
return cli(side, `:put [/system resource get version]; :put [/system resource get board-name];
:put [/ip/service/print as-value proplist=disabled,port,certificate,address where name=www-ssl and dynamic=no];
:put [:len [/interface/gre/find]]; :put [:len [/ip/vrf/find]];
:put [:len [/ip/ipsec/peer/find]]; :put [:len [/ip/ipsec/installed-sa/find]];
:put [:len [/ip/firewall/filter/find]]; :put [:len [/ipv6/firewall/filter/find]];
:put [:len [/ip/address/find where address~${quote("^" + hosts[side] + "/")}]]`)
}
async function port() {
const socket = createServer()
await new Promise<void>((ok, fail) => socket.listen(0, "127.0.0.1", ok).on("error", fail))
const value = (socket.address() as { port: number }).port
await new Promise<void>((ok, fail) => socket.close(error => error ? fail(error) : ok()))
return value
}
async function until<T>(label: string, check: () => T | Promise<T>, timeout = 45000) {
const end = Date.now() + timeout
do {
const value = await check()
if (value) return value
await delay(900)
} while (Date.now() < end)
throw Error(label + " timed out")
}
function prepareGre(side: number) {
const local = hosts[side], remote = hosts[1 - side]
const p = quote(short)
cli(side, `:put "profile"; /ip/ipsec/profile/add name=${p} enc-algorithm=aes-256 hash-algorithm=sha256 dh-group=modp2048;
:put "proposal"; /ip/ipsec/proposal/add name=${p} enc-algorithms=aes-256-cbc auth-algorithms=sha256 pfs-group=modp2048;
:put "peer"; /ip/ipsec/peer/add name=${p} address=${remote}/32 local-address=${local} profile=${p} exchange-mode=ike2 passive=${side ? "yes" : "no"};
:put "identity"; /ip/ipsec/identity/add peer=${p} auth-method=pre-shared-key secret=${quote(psk)} comment=${p};
:put "policy"; /ip/ipsec/policy/add peer=${p} src-address=${local}/32 dst-address=${remote}/32 protocol=gre action=encrypt level=require tunnel=no proposal=${p} comment=${p};
:put "GRE"; /interface/gre/add name=${quote(gre)} local-address=${local} remote-address=${remote} allow-fast-path=no comment=${quote("IX:" + segment)};
/ip/address/add address=${pin(side)}/30 interface=${quote(gre)} comment=${quote(ns + ":lab:gre")}`, "isolated IKEv2/GRE setup")
}
async function openRest(side: number) {
const sessions = cli(side, ':put [/user/active/print as-value proplist=address where name=claude and via=ssh]')
const sources = [...new Set([...sessions.matchAll(/address=([\d.]+)/g)].map(match => match[1]))]
assert(sources.length === 1 && isIPv4(sources[0]), "Cannot pin HTTPS to a single observed management address")
const source = sources[0] + "/32", host = hosts[side], ca = short + "-ca", leaf = short + "-tls"
cli(side, `/certificate/add name=${quote(ca)} common-name=${quote(ca)} key-usage=key-cert-sign,crl-sign days-valid=1;
/certificate/sign [find where name=${quote(ca)}];
/certificate/add name=${quote(leaf)} common-name=${quote(host)} subject-alt-name=IP:${host} key-usage=tls-server days-valid=1;
/certificate/sign [find where name=${quote(leaf)}] ca=${quote(ca)};
/certificate/export-certificate [find where name=${quote(ca)}] file-name=${quote(ca)}`)
const certificate = cli(side, `:put [/file/get [find where name=${quote(ca + ".crt")}] contents]`)
assert.equal(new X509Certificate(certificate).ca, true)
certificates.push(certificate)
setDefaultCACertificates([...roots, ...certificates])
cli(side, `/ip/firewall/filter/add chain=input action=drop protocol=tcp dst-port=443 dst-address=${host}/32 src-address=!${source} comment=${quote(ns + ":lab:tls")};
/user/group/add name=${quote(short)} policy=read,write,api,rest-api;
/user/add name=${quote(short)} address=${source} group=${quote(short)} password=${quote(password)};
/ip/service/set [find where name=www-ssl and dynamic=no] address=${source} certificate=${quote(leaf)} disabled=no`, "restricted HTTPS REST setup")
let lastError = ""
await until("router REST on verified HTTPS", async () => {
const response = await fetch(`https://${host}/rest/system/resource`, {
headers: { authorization: "Basic " + Buffer.from(short + ":" + password).toString("base64") }, signal: AbortSignal.timeout(5000),
}).catch(error => { lastError = String(error.cause ?? error); return undefined })
if (!response) return false
assert.equal(response.status, 200, "REST test user was refused")
assert.equal((await response.json()).version, "7.24.5 (stable)")
return true
}, 18000).catch(error => { throw Error(`CHR ${side + 1}: ${error.message} · ${lastError}`) })
}
function clean(side: number, before: string) {
const own = `where comment~${quote(appliedNamespace ? `^(${ns}|${appliedNamespace}):` : "^" + ns + ":")}`
const checks = ixPaths.map(path => `[:len [${path}/find ${own}]]`)
cli(side, `/ip/service/set [find where name=www-ssl and dynamic=no] disabled=yes address="" certificate=none;
/user/remove [/user/find where name=${quote(short)}]; /user/group/remove [/user/group/find where name=${quote(short)}];
/file/remove [/file/find where name=${quote(short + "-ca.crt")}];
/certificate/remove [/certificate/find where name=${quote(short + "-tls")}];
/certificate/remove [/certificate/find where name=${quote(short + "-ca")}];
${ixPaths.map(path => `${path}/remove [${path}/find ${own}]`).join("; ")};
/interface/gre/remove [/interface/gre/find where name=${quote(gre)} and comment=${quote("IX:" + segment)}];
${ipsecPaths.map((path, i) => `${path}/remove [${path}/find where ${i < 2 ? "comment" : "name"}=${quote(short)}]`).join("; ")}`)
checks.push(...ipsecPaths.map((path, i) => `[:len [${path}/find where ${i < 2 ? "comment" : "name"}=${quote(short)}]]`),
`[:len [/user/find where name=${quote(short)}]]`, `[:len [/user/group/find where name=${quote(short)}]]`,
`[:len [/certificate/find where name~${quote("^" + short + "-(ca|tls)$")}]]`, `[:len [/file/find where name=${quote(short + "-ca.crt")}]]`)
assert.equal(cli(side, ":put (" + checks.join("+") + ")"), "0")
assert.equal(baseline(side), before)
}
async function useManager() {
const root = await mkdtemp(join(tmpdir(), "mmapp-ix-mcp-"))
process.chdir(root)
const pgPort = await port(), httpPort = await port()
const origin = `http://127.0.0.1:${httpPort}`, endpoint = new URL("/mcp", origin)
const pgPassword = randomBytes(24).toString("hex"), jwtSecret = randomBytes(32).toString("hex")
Object.assign(process.env, { DATABASE_URL: `postgresql://ixlab:${pgPassword}@127.0.0.1:${pgPort}/postgres`,
NODE_ENV: "test", AUTH_REQUIRED: "true", AUTH_JWT_SECRET: jwtSecret, AUTH_ISSUER: origin,
AUTH_PORTAL_URL: origin, MCP_STATE_DIR: join(root, "state"), MCP_PUBLIC_URL: endpoint.href, MCP_UI_URL: origin, CORS_ORIGIN: origin })
const pg = new EmbeddedPostgres({ databaseDir: join(root, "db"), user: "ixlab", password: pgPassword, port: pgPort,
persistent: false, createPostgresUser: false, initdbFlags: ["--encoding=UTF8", "--no-locale"], postgresFlags: ["-h", "127.0.0.1"],
onLog: () => {}, onError: () => {} })
await pg.initialise()
await pg.start()
const { pool, closePool } = await import("../../src/db/index.js")
const { applySqlMigrations } = await import("../../src/db/migrate.js")
const { buildApp } = await import("../../src/index.js")
const { updateMcpSettings } = await import("../../src/modules/mcp/store.js")
let app: Awaited<ReturnType<typeof buildApp>> | undefined
const client = new Client({ name: "owned-ix-gre", version: "1.0.0" })
try {
await applySqlMigrations(pool)
const serverIds: number[] = []
for (const [side, host] of hosts.entries()) {
const { rows } = await pool.query(`INSERT INTO servers(name,host,port,username,password,use_ssl,verify_ssl,enabled,type)
VALUES($1,$2,443,$3,$4,true,true,true,'ix-node') RETURNING id`, [`ix-chr-${side + 1}`, host, short, password])
serverIds.push(Number(rows[0].id))
}
const permissions = ["mm:network:read", "mm:network:write", "mm:mcp:admin", "mm:mcp:approve"]
const actor = { id: "ix-fixture", email: "[email protected]", name: "IX fixture", apps: ["mm"], permissions,
expiresAt: Date.now() + 1800000, issuedAt: Date.now() }
await updateMcpSettings({ enabled: true }, actor)
app = await buildApp({ startScheduler: false, logger: false })
await app.listen({ port: httpPort, host: "127.0.0.1" })
const jwt = await new SignJWT({ sub: actor.id, apps: actor.apps, permissions }).setProtectedHeader({ alg: "HS256" })
.setIssuer(origin).setIssuedAt().setExpirationTime("15m").sign(new TextEncoder().encode(jwtSecret))
async function rest(path: string, data?: object) {
const response = await fetch(origin + "/api" + path, { method: data ? "POST" : "GET",
headers: { authorization: "Bearer " + jwt, "content-type": "application/json" },
...(data ? { body: JSON.stringify(data) } : {}), signal: AbortSignal.timeout(15000) })
const body = await response.json()
assert(response.ok, `${path}: ${response.status} ${JSON.stringify(body).slice(0, 600)}`)
return body
}
const configuration = { name: segment, sites: serverIds.map((serverId, side) => ({
serverId, routerId: pin(side), autonomousSystem: 64512 + side,
interfaces: [{ name: gre, kind: "gre-ipsec", localEndpoint: hosts[side], endpoint: hosts[1 - side], peerServerId: serverIds[1 - side] }],
})) }
const draft = await rest("/ix/segments", { revision: 0, configuration })
const segmentId: string = draft.segment.id
assert.match(segmentId, /^[0-9a-f-]{36}$/)
appliedNamespace = ixNamespace(segmentId)
assert.equal(draft.segment.revision, 1)
assert.equal(draft.segment.appliedRevision, null)
result.managerRestDraft = true
const { token } = await rest("/mcp/connections", { name: "Owned IX approval", mode: "approval", permissions: ["mm:network:read", "mm:network:write"],
limits: { mutationsPerHour: 40, callsPerMinute: 120 },
expiresAt: new Date(Date.now() + 600000).toISOString(), scope: { serverIds, allowGlobal: false,
operationIds: ["network.ix.configure", "network.ix.read", "changes.apply", "operations.read"] } })
await client.connect(new StreamableHTTPClientTransport(endpoint, {
requestInit: { headers: { authorization: "Bearer " + token } },
}), { timeout: 10000 })
async function call(name: string, args: object, expectedError = false) {
const reply = await client.callTool({ name, arguments: args }, undefined, { timeout: 120000 })
assert.equal(Boolean(reply.isError), expectedError, name + " " + JSON.stringify(reply.structuredContent ?? reply.content).slice(0, 1200))
return (reply.structuredContent as { result: any })?.result
}
const plan = await call("mm_ix_change_plan", { change: { id: segmentId, revision: 1, configuration } })
assert.equal(plan.state, "awaiting-approval")
const apply = { planId: plan.planId, idempotencyKey: randomUUID() }
await call("mm_change_apply", apply, true)
await rest("/mcp/plans/" + plan.planId + "/decision", { approve: true })
const op = await call("mm_change_apply", apply)
const terminal = await until("MCP terminal operation", async () => {
const value = await call("mm_operation_get", { operationId: op.id })
return !["pending", "running"].includes(value.state) && value
}, 90000)
assert.equal(terminal.state, "applied", JSON.stringify({ state: terminal.state, error: terminal.error_code }))
assert.equal(terminal.verification, "passed")
assert.equal((await call("mm_change_apply", apply)).id, op.id)
const verified = await call("mm_ix_segment_get", { id: segmentId })
assert.equal(verified.segment.appliedRevision, 2)
const status = await rest("/ix/segments/" + segmentId + "/status")
assert.deepEqual(status.sites.map(({ serverId, available }: { serverId: number; available: boolean }) => [serverId, available]),
serverIds.map(serverId => [serverId, true]))
result.mcpAppliedAndVerified = true
result.routerRestSites = status.sites.length
} finally {
await client.close()
await app?.close()
await closePool()
await pg.stop()
}
}
function packets(side: number) {
assert(appliedNamespace)
return Number(cli(side, `:put [/ping address=${pin(1 - side)} src-address=${pin(side)} vrf=${appliedNamespace} count=3 interval=250ms]`).split(/\r?\n/).at(-1))
}
let failure: unknown
try {
const original = sides.map(baseline)
for (const value of original) {
const [version, board, ...expected] = value.split(/\r?\n/)
assert.equal(version, "7.24.5 (stable)")
assert.match(board, /^CHR/)
const [https, ...inventory] = expected
assert.match(https, /(?:^|;)certificate=none;disabled=true;port=443$/)
assert.deepEqual(inventory, ["0", "1", "0", "0", "0", "0", "1"])
}
for (const side of sides) {
entered.set(side, original[side])
prepareGre(side)
}
for (const side of sides) await openRest(side)
console.log("Both RouterOS REST endpoints authenticated with temporary trusted HTTPS certificates")
await useManager()
console.log("MCP applied the reviewed configuration to both routers")
await until("encrypted GRE forwarding", () => packets(0) === 3 && packets(1) === 3)
const sas = sides.map(side => Number(cli(side, ":put [:len [/ip/ipsec/installed-sa/find where state=mature]]")))
assert(sas.every(count => count === 2))
result.greReplies = [3, 3]
result.matureIpsecSAs = sas
// Turn off both matching policies during the plaintext negative test.
for (const side of sides) cli(side, `/ip/ipsec/policy/disable [/ip/ipsec/policy/find where comment=${quote(short)}]`)
for (const side of sides) assert.equal(packets(side), 0)
result.plaintextGreBlocked = true
for (const side of sides) cli(side, `/ip/ipsec/policy/enable [/ip/ipsec/policy/find where comment=${quote(short)}]`)
await until("GRE recovery with required IPsec", () => packets(0) === 3 && packets(1) === 3)
result.greRecovered = true
} catch (error) {
failure = error
}
setDefaultCACertificates(roots)
let testStandRestored = true
for (const [side, original] of entered) {
try { clean(side, original) } catch (error) {
console.error(`Cleanup unconfirmed on CHR ${side + 1} · ${ns} ${appliedNamespace ?? ""}`)
testStandRestored = false
failure ??= error
}
}
console.log(JSON.stringify({ ...result, testStandRestored }))
if (failure) throw failure
+198
View File
@@ -0,0 +1,198 @@
import assert from "node:assert/strict"
import { randomInt, randomUUID } from "node:crypto"
import { setTimeout as delay } from "node:timers/promises"
import { ixSegmentConfigurationSchema } from "@mmapp/contracts/internet-exchange"
import { compileIxConfiguration, ixDiscardInterface, ixNamespace } from "../../src/services/internet-exchange-plan.js"
import { cli, hosts, quote } from "./ix-chr-ssh.mjs"
assert.equal(process.argv[2], "--owned-test-pair")
assert(process.argv.length === 3 || (process.argv.length === 4 && process.argv[3] === "--ipv6"))
const ipv6 = process.argv[3] === "--ipv6"
const id = randomUUID(), ns = ixNamespace(id), short = ns.slice(0, 10)
const discard = ixDiscardInterface(id)
const marker = ns + ":lab", primary = short + "-p", backup = short + "-b"
const port = randomInt(45000, 55000)
const net = "10.247." + randomInt(32, 224)
const [aP, bP, aB, bB] = ipv6 ? ["fd00:247:1::1", "fd00:247:1::2", "fd00:247:2::1", "fd00:247:2::2"] : [1, 2, 5, 6].map(n => net + "." + n)
const [target, pProbe, bProbe, trafficSource] = ipv6 ? ["2001:db8:3::200", "2001:db8:3::201", "2001:db8:3::202", "2001:db8:3::203"] : ["192.0.2.200", "192.0.2.201", "192.0.2.202", "192.0.2.203"]
const afi = ipv6 ? "ipv6" : "ip", length = ipv6 ? "/128" : "/32", gateway = ipv6 ? "fd00:198:18::101" : "198.18.0.101"
const addressPath = "/" + afi + "/address", routePath = "/" + afi + "/route", filterPath = "/" + afi + "/firewall/filter", listPath = "/" + afi + "/firewall/address-list"
const advertise: Record<string, string> = ipv6 ? { advertise: "no" } : {}
const community = "65000:120"
const entered = new Set<number>()
const results: Record<string, unknown> = {}
const pending: string[][] = [[], []]
const fields = (values: Record<string, string>) => Object.entries(values).map(([name, value]) => name === "blackhole" && value === "yes" ? name : name + "=" + quote(value)).join(" ")
function add(side: number, path: string, values: Record<string, string>, beforeMain = false) {
pending[side].push(path + " add " + fields(values) + (beforeMain ? " place-before=[/ip/vrf/find where name=main]" : ""))
}
function flush(side: number) {
while (pending[side].length) {
const commands = pending[side].splice(0, 10)
cli(side, commands.map((command, index) => `:put "IX_STAGE_${index}"; ${command}`).join("; "))
}
}
function owned(side: number, path: string, values: Record<string, string>, tag: string) {
add(side, path, { ...values, comment: marker + ":" + tag })
}
function preflight(side: number) {
const data = cli(side, `:put [/system resource get version]; :put [/system resource get board-name];
:put ([:len [/ip/vrf/find]]=1 && [:len [/interface/wireguard/find]]=0 && [:len [/routing/bgp/connection/find]]=0 && [:len [/ip/firewall/filter/find]]=0 && [:len [/ipv6/firewall/filter/find]]=0);
:put [:len [/ip/address/find where address~${quote("^" + hosts[side] + "/")}]]`)
const [version, board, empty, address] = data.split(/\r?\n/)
assert.equal(version, "7.24.5 (stable)")
assert.match(board, /^CHR/)
assert.equal(empty, "true", "Stand changed, refusing a modified baseline")
assert.equal(address, "1", "Expected test WAN address missing")
}
const config = ixSegmentConfigurationSchema.parse({
name: "ix-acceptance",
sites: [
{ serverId: 501, autonomousSystem: 64512, routerId: net + ".5",
interfaces: [{ name: primary, kind: "wireguard", peerServerId: 502 }, { name: backup, kind: "wireguard", peerServerId: 502 }],
groups: [{ id: "far", gateway, primary: { interface: primary, nextHop: bP, probe: pProbe },
backup: { interface: backup, nextHop: bB, probe: bProbe } }],
neighbors: [{ id: "feed", type: "public-feed", address: bB, localAddress: aB, remoteAs: 64513, binding: backup, rules: [{ community, group: "far" }] }],
},
{ serverId: 502, autonomousSystem: 64513, routerId: net + ".6",
interfaces: [{ name: primary, kind: "wireguard", peerServerId: 501 }, { name: backup, kind: "wireguard", peerServerId: 501 }] },
],
})
const compiled = compileIxConfiguration(id, config)
const deferred = new Set([routePath, "/routing/bgp/instance", "/routing/filter/rule", "/routing/bgp/connection"])
const cleanupPaths = [...deferred].reverse().concat(listPath,
"/interface/wireguard/peers", addressPath, "/ip/firewall/filter", "/ipv6/firewall/filter", "/ip/vrf", "/interface/bridge", "/interface/wireguard")
function clean(side: number) {
const where = `where comment~${quote("^" + ns + ":")}`
cli(side, cleanupPaths.map(path => `${path} remove [${path}/find ${where}]`).join("; "))
assert.equal(cli(side, `:put (${cleanupPaths.map(path => `[:len [${path}/find ${where}]]`).join("+")})`), "0")
preflight(side)
}
function observe() {
return cli(0, `:put [/routing/route/print as-value where dst-address=${quote(target + length)} and routing-table=${quote(ns)}]`)
}
async function until(label: string, check: () => boolean, timeout = 75000) {
const start = Date.now()
while (!check()) {
if (Date.now() - start > timeout) throw new Error(label + " timeout: " + observe())
await delay(900)
}
return Date.now() - start
}
function hasPath(name: string) {
const fields = observe().split(";")
return fields.includes("bgp=true") && fields.includes("active=true") && fields.some(field => field.startsWith("immediate-gw=") && field.endsWith("%" + name))
}
function establishedSession() {
const session = cli(0, `:put [/routing/bgp/session/print as-value where name~${quote(ns + "-feed")}]`).split(";")
assert.ok(session.includes("established=true"))
assert.ok(session.includes("prefix-count=1"))
const started = session.find(field => field.startsWith("last-started="))
assert.ok(started)
return started
}
function packetProbe(destination: string, source?: string, side = 0, routingTable = ns) {
return Number(cli(side, `:put [/ping address=${destination} vrf=${routingTable} count=3 interval=250ms${source ? " src-address=" + source : ""}]`).split(/\r?\n/).at(-1))
}
async function run() {
preflight(0); preflight(1)
for (let side = 0; side < 2; side++) {
entered.add(side)
for (const [index, name] of [primary, backup].entries()) owned(side, "/interface/wireguard", { name, "listen-port": String(port + index), vrf: "main" }, "wg" + index)
for (const object of compiled.filter(object => object.serverId === 501 + side && !deferred.has(object.path))) {
add(side, object.path, object.fields, object.path === "/ip/vrf")
if (object.path === "/ip/vrf") {
flush(side)
await until("VRF interface", () => cli(side, `:put [:len [/interface/find where name=${quote(ns)}]]`) === "1", 9000)
}
}
for (const [index, name] of [primary, backup].entries()) {
owned(side, "/ip/firewall/filter", { chain: "input", action: "accept", protocol: "udp", "src-address": hosts[1 - side] + "/32", "dst-port": String(port + index) }, "allow-wg" + index)
const inner = (side ? [bP, bB] : [aP, aB])[index]
owned(side, addressPath, { address: inner + (ipv6 ? "/64" : "/30"), interface: name, ...advertise }, "address" + index)
}
flush(side)
}
for (const [index, name] of [primary, backup].entries()) {
const keys = [0, 1].map(side => cli(side, `:put [/interface/wireguard/get [/interface/wireguard/find where name=${quote(name)}] public-key]`))
keys.forEach(value => assert.match(value, /^[A-Za-z0-9+/]{43}=$/))
for (const side of [0, 1]) owned(side, "/interface/wireguard/peers", { interface: name, "public-key": keys[1 - side], "endpoint-address": hosts[1 - side],
"endpoint-port": String(port + index), "allowed-address": (side
? [index ? aB : aP, trafficSource]
: [index ? bB : bP, target, index ? bProbe : pProbe]).map(address => address + length).join(","),
"persistent-keepalive": "5" }, "peer" + index)
}
for (const [index, address] of [target, pProbe, bProbe].entries()) {
owned(1, addressPath, { address: address + length, interface: discard, ...advertise }, "destination" + index)
}
owned(0, addressPath, { address: trafficSource + length, interface: discard, ...advertise }, "source")
// Keep the same source and return path across both forward paths.
owned(1, routePath, { "dst-address": trafficSource + length, "routing-table": ns,
gateway: aB + "%" + backup + "@" + ns, "target-scope": "10" }, "return")
const control = { chain: "input", "in-interface": ns, protocol: "tcp", "src-address": aB + length, action: "accept" }
const beforeDeny = " place-before=[" + filterPath + "/find where comment=" + quote(ns + ":deny-input-" + (ipv6 ? "6" : "4")) + "]"
cli(1, [fields({ ...control, "dst-port": "179", comment: marker + ":bgp-accept" }),
fields({ ...control, "src-port": "179", "connection-state": "established", comment: marker + ":bgp-reply" })]
.map(rule => filterPath + " add " + rule + beforeDeny).join("; "))
owned(1, listPath, { list: short + "-net", address: target + length }, "network")
owned(1, "/routing/bgp/instance", { name: ns, as: "64513", "router-id": net + ".6", vrf: ns, "routing-table": ns }, "instance")
owned(1, "/routing/filter/rule", { chain: short + "-out", rule: `if (dst == ${target}${length}) { set bgp-communities ${community}; accept; } reject;` }, "export")
owned(1, "/routing/bgp/connection", { name: short + "-export", instance: ns, afi, "local.role": "ebgp", "local.address": bB,
"remote.address": aB + length, "remote.as": "64512", vrf: ns, "routing-table": ns, "output.network": short + "-net", "output.filter-chain": short + "-out" }, "connection")
for (const object of compiled.filter(object => object.serverId === 501 && deferred.has(object.path))) add(0, object.path, object.fields)
flush(1); flush(0)
await until("primary WireGuard transport", () => packetProbe(bP, aP) === 3, 40000)
await until("backup WireGuard transport", () => packetProbe(bB, aB) === 3, 40000)
await until("BGP primary route", () => hasPath(primary))
const session = establishedSession()
results.bgpEstablished = true
results.primaryRoute = observe()
results.primaryReplies = packetProbe(target, trafficSource)
assert.equal(results.primaryReplies, 3)
assert.equal(cli(0, `:put [:len [/routing/route/find where dst-address=${quote(target + length)} and routing-table=main]]`), "0", "BGP prefix must not leak to main")
results.mainIsolated = true
// Drop only A's test UDP flow on the remote end. Interfaces remain enabled,
// so this exercises probe-driven failover rather than administrative down.
cli(1, "/ip/firewall/filter add " + fields({ chain: "input", action: "drop", protocol: "udp", "src-address": hosts[0] + "/32",
"dst-port": String(port), comment: marker + ":cut" }) + " place-before=[/ip/firewall/filter/find where comment=" + quote(marker + ":allow-wg0") + "]")
results.failoverMs = await until("primary underlay loss", () => hasPath(backup))
results.backupRoute = observe()
results.backupReplies = packetProbe(target, trafficSource)
assert.equal(results.backupReplies, 3)
assert.equal(establishedSession(), session)
cli(1, "/ip/firewall/filter remove [/ip/firewall/filter/find where comment=" + quote(marker + ":cut") + "]")
results.failbackMs = await until("primary recovery", () => hasPath(primary))
results.recoveredReplies = packetProbe(target, trafficSource)
assert.equal(results.recoveredReplies, 3)
assert.equal(establishedSession(), session)
results.bgpSessionKept = true
}
let failure: unknown
try { await run() } catch (error) {
failure = error
for (const side of entered) try {
console.error(`Test CHR ${side + 1} · ${cli(side, `:put [/routing/bgp/session/print as-value];
:put [/interface/wireguard/peers/print as-value proplist=interface,rx,tx,last-handshake,current-endpoint-address where comment~${quote("^" + ns + ":")}];
${filterPath}/print stats where comment~${quote("^" + ns + ":")};
:put [${addressPath}/print as-value proplist=address,interface,invalid where comment~${quote("^" + ns + ":")}];
:put [/routing/route/print as-value where routing-table=${quote(ns)}]; :put [/routing/nexthop/print as-value where address~${quote(gateway)}]`).slice(0, 8500)}`)
} catch { /* Preserve the original failure and continue cleanup. */ }
}
for (const side of entered) {
try { clean(side) } catch (error) {
console.error(`Cleanup unconfirmed on test CHR ${side + 1} for ${ns}: ${error}`)
failure ??= error
}
}
if (failure) {
console.error(JSON.stringify(results))
throw failure
}
console.log(JSON.stringify({ ...results, testStandRestored: true }))
@@ -0,0 +1,97 @@
import assert from "node:assert/strict"
import { mkdtemp, readdir, readFile } from "node:fs/promises"
import { tmpdir } from "node:os"
import { join, resolve } from "node:path"
import { createServer } from "node:net"
import { randomBytes } from "node:crypto"
import EmbeddedPostgres from "embedded-postgres"
import { FIXED59_IX_DELTA } from "../writer-profile/fixed59-ix-delta.js"
import { canonical } from "../writer-profile/application-writer-profile-canonical.js"
import { ixWriterProfileEnrollmentSql } from "../../src/db/internet-exchange-writer-profile.js"
import { FIXED44_CATALOGUE_BUILD_SQL } from "../writer-profile/fixed44-build-sql.js"
import { CatalogueValueStream } from "../writer-profile/application-writer-profile-catalogue-values.js"
import { projectCatalogueData, GROUPS, sorted, type Inventory, type Row } from "../writer-profile/catalogue-projection.js"
const root = await mkdtemp(join(tmpdir(), "mmapp-ix-profile-"))
assert.equal(resolve(root, ".."), resolve(tmpdir()))
const listener = createServer()
await new Promise<void>((done, reject) => listener.listen(0, "127.0.0.1", done).on("error", reject))
const port = (listener.address() as { port: number }).port
await new Promise<void>((done, reject) => listener.close(error => error ? reject(error) : done()))
const app = new EmbeddedPostgres({ databaseDir: root, user: "ixlab", password: randomBytes(24).toString("hex"),
port, persistent: false, createPostgresUser: false, initdbFlags: ["--encoding=UTF8", "--no-locale"],
postgresFlags: ["-h", "127.0.0.1", "-c", "jit=off"], onLog: () => {}, onError: () => {} })
const roles = ["ix_operator", "ix_runtime", "ix_capture", "ix_publisher", "ix_control"]
const tables = ["ix_segments", "ix_segment_devices", "ix_segment_revisions"]
console.log("Initialising disposable PostgreSQL")
await app.initialise()
console.log("Starting disposable PostgreSQL")
await app.start()
const db = app.getPgClient("postgres", "127.0.0.1")
try {
await db.connect()
console.log("Applying pinned migrations in the fixture")
await db.query("CREATE TABLE schema_migrations(id text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now())")
const directory = resolve("backend/drizzle")
for (const name of (await readdir(directory)).filter(name => name.endsWith(".sql")).sort()) {
await db.query(await readFile(join(directory, name), "utf8"))
await db.query("INSERT INTO schema_migrations(id) VALUES ($1) ON CONFLICT DO NOTHING", [name.slice(0,-4)])
}
for (const role of roles) await db.query("CREATE ROLE " + role)
await db.query("CREATE SCHEMA IF NOT EXISTS mmapp_control; CREATE FUNCTION mmapp_control.candidate_truncate_preadmitted() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RETURN NULL; END; $$")
for (const name of tables) await db.query("ALTER TABLE public." + name + " OWNER TO ix_operator")
await db.query("GRANT USAGE ON SCHEMA mmapp_control TO ix_operator; GRANT EXECUTE ON FUNCTION mmapp_control.candidate_truncate_preadmitted() TO ix_operator")
await db.query("BEGIN; SET LOCAL ROLE ix_operator")
await db.query(ixWriterProfileEnrollmentSql("ix_runtime", "ix_capture"))
await db.query("COMMIT")
const insert = "INSERT INTO ix_segments(id,name,revision,configuration) VALUES ('8dd5d978-83ac-4343-8cff-16655a0a77a7','fixture',1,'{}')"
const maintenance = app.getPgClient("postgres", "127.0.0.1")
await maintenance.connect()
try {
await maintenance.query("SELECT pg_advisory_lock(8723110::bigint)")
await db.query("BEGIN; SET LOCAL ROLE ix_runtime; SET LOCAL lock_timeout='250ms'")
await assert.rejects(db.query(insert), { code: "55P03" })
await db.query("ROLLBACK")
} finally {
await maintenance.end()
}
await db.query("BEGIN; SET LOCAL ROLE ix_runtime; SELECT pg_advisory_xact_lock_shared(8723110::bigint)")
await db.query(insert)
await db.query("INSERT INTO ix_segment_revisions(segment_id,revision,configuration,actor_id) VALUES ('8dd5d978-83ac-4343-8cff-16655a0a77a7',1,'{}','fixture')")
await assert.rejects(db.query("UPDATE ix_segment_revisions SET actor_id='other'"), { code: "42501" })
await db.query("ROLLBACK")
console.log("Capturing exact IX catalogue rows")
await db.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY; SET LOCAL search_path=pg_catalog,public; SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'")
const stream = new CatalogueValueStream()
const raw = {} as Inventory
for (const group of GROUPS) raw[group] = []
const statement = FIXED44_CATALOGUE_BUILD_SQL.replace("LIMIT 104", "LIMIT 113")
await db.query("DECLARE ix_capture_cursor NO SCROLL CURSOR WITHOUT HOLD FOR " + statement, roles)
for (;;) {
const page = (await db.query("FETCH FORWARD 128 FROM ix_capture_cursor")).rows
if (!page.length) break
for (const record of stream.acceptPage(page)) {
if (record.kind === "global") raw[record.identity === "base_tables" ? "tables" : record.identity] = record.value.rows as Row[]
if (record.kind === "base") for (const group of GROUPS) raw[group].push(...record.value[group] as Row[])
}
}
stream.finish()
const projected = projectCatalogueData(raw)
const delta: Inventory = {}
for (const group of ["tables", ...GROUPS]) delta[group] = sorted(projected[group].filter(row => tables.includes(String(row.relname))))
delta.constraints.push(...projected.constraints.filter(row => row.relname === "servers" && row.conname === "servers_type_check"))
delta.triggers.push(...projected.triggers.filter(row => row.relname === "servers" && String(row.definition).includes("ix_segment_devices")))
assert.equal(delta.tables.length, 3)
assert.equal(delta.triggers.filter(row => row.relname === "servers").length, 2)
await db.query("ROLLBACK")
let text = JSON.stringify(delta, null, 2)
for (const [index, role] of roles.entries()) text = text.replaceAll(role, "@mm-role:" + ["operator","runtime","capture","publisher","control"][index] + "@")
assert.equal(canonical(JSON.parse(text)), canonical(FIXED59_IX_DELTA), "PostgreSQL catalogue drifted from source59")
console.log(JSON.stringify({ postgres: (await db.query("SHOW server_version")).rows[0].server_version, relations: delta.tables.length, constraints: delta.constraints.length,
triggers: delta.triggers.length, leastPrivilege: true, sharedWriterLock: true }))
} finally {
await db.end().catch(() => {})
await app.stop()
}
@@ -20,10 +20,11 @@ test('fixed artifact is reproducible, resolves the compiled backend graph, and r
const packageJson=JSON.parse(await readFile(path.join(backend,'package.json'),'utf8'))
await mkdir(projectBackend,{recursive:true});await writeFile(path.join(projectBackend,'package.json'),JSON.stringify({type:'module',dependencies:packageJson.dependencies}))
const first=await buildDatabaseOperator({backendDirectory:backend,outputDirectory:output}),second=await buildDatabaseOperator({backendDirectory:backend,outputDirectory:output})
assert.deepEqual(first,second);assert.equal(first.modules.length,OPERATOR_SOURCES.length);assert.equal(first.modules.length,31);assert.deepEqual(first.runtimeDependencies,['dotenv','pg','zod']);assert.equal(first.backendDistModules.length,30)
assert.deepEqual(first,second);assert.equal(first.modules.length,OPERATOR_SOURCES.length);assert.equal(first.modules.length,33);assert.deepEqual(first.runtimeDependencies,['dotenv','pg','zod']);assert.equal(first.backendDistModules.length,32)
assert(first.modules.some(item=>item.file==='writer-profile/registry-enrollment.mjs'))
assert(first.modules.some(item=>item.file==='writer-profile/marker-fence-enrollment.mjs'))
assert(first.modules.some(item=>item.file==='writer-profile/fixed58-build-definition.js'))
assert(first.modules.some(item=>item.file==='writer-profile/fixed59-build-definition.js'))
assert(first.backendDistImports.includes('db/database-restore-quarantine-profile.js'))
assert(first.backendDistImports.includes('db/database-source-activation-record.js'))
assert(first.backendDistImports.includes('services/server-probe-legacy-source-inventory.js'))
@@ -13,5 +13,5 @@ export const OPERATOR_SOURCES=Object.freeze([
'writer-profile/catalogue.mts','writer-profile/catalogue-value-bridge.ts','writer-profile/catalogue-projection.ts','writer-profile/catalogue-leaves.ts',
'writer-profile/application-writer-profile-catalogue.ts','writer-profile/application-writer-profile-catalogue-values.ts',
'writer-profile/application-writer-profile-catalogue-parent-policy.ts','writer-profile/application-writer-profile-canonical.ts',
'writer-profile/fixed44-build-sql.ts','writer-profile/fixed44-build-definition.ts','writer-profile/fixed56-build-definition.ts','writer-profile/fixed56-build-delta.ts','writer-profile/fixed57-build-definition.ts','writer-profile/fixed58-build-definition.ts','writer-profile/expanded44-data-policy.ts',
'writer-profile/fixed44-build-sql.ts','writer-profile/fixed44-build-definition.ts','writer-profile/fixed56-build-definition.ts','writer-profile/fixed56-build-delta.ts','writer-profile/fixed57-build-definition.ts','writer-profile/fixed58-build-definition.ts','writer-profile/fixed59-build-definition.ts','writer-profile/fixed59-ix-delta.ts','writer-profile/expanded44-data-policy.ts',
])
@@ -57,11 +57,11 @@ export async function main(args:string[]){
// import so a newly loaded NODE_PG_FORCE_NATIVE cannot select another driver.
const {createProtectedDatabaseSourceBindingVerifier}=await import('./writer-profile/protected-source-registry.mjs')
assertJsDriverEnvironment(process.env)
const [{default:pg},{createConnectedPublisher,requireCurrentCatalogueDefinition},{collectSource44Catalogue,collectSource56Catalogue,collectSource57Catalogue,collectSource58Catalogue,approvedDefinition}]=await Promise.all([
const [{default:pg},{createConnectedPublisher,requireCurrentCatalogueDefinition},{collectSource44Catalogue,collectSource56Catalogue,collectSource57Catalogue,collectSource58Catalogue,collectSource59Catalogue,approvedDefinition}]=await Promise.all([
import('pg'),import('./writer-profile/publisher.mjs'),import('./writer-profile/catalogue.mjs')])
assertJsDriverEnvironment(process.env)
const reviewedDefinition=legacyInspection?undefined:approvedDefinition(definition)
const collectCatalogues={44:collectSource44Catalogue,56:collectSource56Catalogue,57:collectSource57Catalogue,58:collectSource58Catalogue}
const collectCatalogues={44:collectSource44Catalogue,56:collectSource56Catalogue,57:collectSource57Catalogue,58:collectSource58Catalogue,59:collectSource59Catalogue}
const collectCatalogue=collectCatalogues[reviewedDefinition&&'edition' in reviewedDefinition?reviewedDefinition.edition:44]
if(!legacyInspection&&!catalogueInspection){
assert.equal(reviewedDefinition!.definitionSha256,pinned!.definitionSha256)
@@ -7,6 +7,7 @@ import {BUILD44_PROVENANCE,FIXED44_ROLE_TEMPLATE} from './fixed44-build-definiti
import {BUILD56_PROVENANCE,FIXED56_ROLE_TEMPLATE} from './fixed56-build-definition.js'
import {BUILD57_PROVENANCE,FIXED57_ROLE_TEMPLATE} from './fixed57-build-definition.js'
import {BUILD58_PROVENANCE,FIXED58_ROLE_TEMPLATE,normalizeFixed58RestoreRows} from './fixed58-build-definition.js'
import {BUILD59_PROVENANCE,FIXED59_ROLE_TEMPLATE} from './fixed59-build-definition.js'
import {GLOBALS,GROUPS,object,rows,sorted,text,projectCatalogueData,type CatalogueEdition,type Inventory,type Row,type Json} from './catalogue-projection.js'
import {validateCatalogueLeafData} from './catalogue-leaves.js'
import {catalogueTopologyPolicy,Expanded44TopologyBudgetData} from './expanded44-data-policy.js'
@@ -20,6 +21,7 @@ const definitions = {
56: { provenance: BUILD56_PROVENANCE, template: FIXED56_ROLE_TEMPLATE },
57: { provenance: BUILD57_PROVENANCE, template: FIXED57_ROLE_TEMPLATE },
58: { provenance: BUILD58_PROVENANCE, template: FIXED58_ROLE_TEMPLATE },
59: { provenance: BUILD59_PROVENANCE, template: FIXED59_ROLE_TEMPLATE },
}
const exact=(a:unknown,b:unknown,label:string)=>{if(canonical(a)!==canonical(b))throw Error('Writer catalogue refused: '+label)}
function captureBindings(input:InstallationRoleBindingsData):Readonly<Record<LogicalRole,string>> {
@@ -68,7 +70,7 @@ export class Fixed44CatalogueDataVerifier {
if(this.counts||table.relispartition!==false||this.bases.has(record.identity))throw Error('Duplicate/late base')
const header=this.headers!.find(t=>t.relname===record.identity);if(!header)throw Error('Foreign base refused');exact(table,header,'header/table')
const actual=projectCatalogueData({...this.globals,...groups,tables:this.headers!})
for(const name of GROUPS)exact(this.edition===58?normalizeFixed58RestoreRows(record.identity,name,actual[name]):actual[name],
for(const name of GROUPS)exact(this.edition>=58?normalizeFixed58RestoreRows(record.identity,name,actual[name]):actual[name],
this.expected[name].filter(r=>r.relname===record.identity),'base '+name+' '+record.identity)
this.topology.acceptRelation('base',record.identity,groups);this.bases.add(record.identity);if(fixedCatalogueParentPeriodData(record.identity))this.templates.set(record.identity,{table,groups});return
}
@@ -4,7 +4,7 @@ export type Json=null|boolean|number|string|Json[]|{[key:string]:Json}
export type Row=Record<string,Json>
export type Inventory=Record<string,Row[]>
export const GROUPS=['columns','triggers','constraints','indexes','roleGrants','inheritance'] as const
export type CatalogueEdition = 44 | 56 | 57 | 58
export type CatalogueEdition = 44 | 56 | 57 | 58 | 59
export const GLOBALS=['database','effectiveDatabase','effectiveFunctions','effectiveSchemas','effectiveSequences','events','functions','identities','memberships','parameters','roles','schemas','settings','sequences'] as const
export const sorted=(rows:Row[])=>rows.slice().sort((a,b)=>canonical(a)<canonical(b)?-1:canonical(a)>canonical(b)?1:0)
export const omit=(row:Row,names:readonly string[]):Row=>Object.fromEntries(Object.entries(row).filter(([key])=>!names.includes(key)))
+9 -2
View File
@@ -5,6 +5,7 @@ import {BUILD44_PROVENANCE,FIXED44_ROLE_TEMPLATE} from './fixed44-build-definiti
import {BUILD56_PROVENANCE,FIXED56_ROLE_TEMPLATE} from './fixed56-build-definition.js'
import {BUILD57_PROVENANCE,FIXED57_ROLE_TEMPLATE} from './fixed57-build-definition.js'
import {BUILD58_PROVENANCE,FIXED58_ROLE_TEMPLATE} from './fixed58-build-definition.js'
import {BUILD59_PROVENANCE,FIXED59_ROLE_TEMPLATE} from './fixed59-build-definition.js'
import {assertRestoreProfileQuarantine} from '../../src/db/database-restore-quarantine-profile.js'
import {canonical,sha} from './application-writer-profile-canonical.js'
import type {CatalogueEdition} from './catalogue-projection.js'
@@ -23,6 +24,10 @@ export function approvedDefinition(value:unknown){
assert.equal(canonical(v.provenance),canonical(BUILD58_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED58_ROLE_TEMPLATE))
return Object.freeze({edition:58 as const,definitionSha256:BUILD58_PROVENANCE.templateSha256,manifestSha256:BUILD58_PROVENANCE.manifestSha256})
}
if(v.format==='application-writer-source59-definition-approval-v1'){
assert.equal(canonical(v.provenance),canonical(BUILD59_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED59_ROLE_TEMPLATE))
return Object.freeze({edition:59 as const,definitionSha256:BUILD59_PROVENANCE.templateSha256,manifestSha256:BUILD59_PROVENANCE.manifestSha256})
}
assert.equal(v.format,'application-writer-source56-definition-approval-v1')
assert.equal(canonical(v.provenance),canonical(BUILD56_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED56_ROLE_TEMPLATE))
return Object.freeze({edition:56 as const,definitionSha256:BUILD56_PROVENANCE.templateSha256,manifestSha256:BUILD56_PROVENANCE.manifestSha256})
@@ -31,11 +36,13 @@ type CatalogueSession = {query:(sql:string,values?:unknown[])=>Promise<{rows:Rec
type CatalogueInstallation = {roleBindings:ConstructorParameters<typeof Fixed44CatalogueValueBridgeData>[0]}
assert.equal(FIXED44_CATALOGUE_BUILD_SQL.split('LIMIT 104').length,2)
const fixed56Sql = FIXED44_CATALOGUE_BUILD_SQL.replace('LIMIT 104','LIMIT 110')
const fixed59Sql = FIXED44_CATALOGUE_BUILD_SQL.replace('LIMIT 104','LIMIT 113')
/** Real incremental DATA verifier on caller's retained transaction. No pool or attestor. */
export const collectSource44Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 44)
export const collectSource56Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 56)
export const collectSource57Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 57)
export const collectSource58Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 58)
export const collectSource59Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 59)
async function collectCatalogue(s:CatalogueSession,installation:CatalogueInstallation,edition:CatalogueEdition){
const settings=(await s.query("SELECT pg_catalog.current_setting('search_path') AS path,pg_catalog.current_setting('TimeZone') AS zone,pg_catalog.current_setting('DateStyle') AS dates")).rows
assert.equal(settings.length,1);assert(['path','zone','dates'].every(k=>typeof settings[0][k]==='string'))
@@ -43,9 +50,9 @@ async function collectCatalogue(s:CatalogueSession,installation:CatalogueInstall
try{
await s.query("SELECT pg_catalog.set_config('search_path','pg_catalog, public',true),pg_catalog.set_config('TimeZone','UTC',true),pg_catalog.set_config('DateStyle','ISO, YMD',true)")
const bindings=installation.roleBindings,values=['operator','runtime','capture','publisher','control'].map(k=>bindings[k as keyof typeof bindings].name)
if(edition===58)await assertRestoreProfileQuarantine(s,bindings.runtime.name,bindings.operator.name,bindings.control.name)
if(edition>=58)await assertRestoreProfileQuarantine(s,bindings.runtime.name,bindings.operator.name,bindings.control.name)
const bridge=new Fixed44CatalogueValueBridgeData(bindings,edition)
await s.query('DECLARE mm_publisher44 NO SCROLL CURSOR WITHOUT HOLD FOR '+(edition===44?FIXED44_CATALOGUE_BUILD_SQL:fixed56Sql),values);cursor=true
await s.query('DECLARE mm_publisher44 NO SCROLL CURSOR WITHOUT HOLD FOR '+(edition===44?FIXED44_CATALOGUE_BUILD_SQL:edition===59?fixed59Sql:fixed56Sql),values);cursor=true
for(;;){const page=(await s.query('FETCH FORWARD 128 FROM mm_publisher44')).rows;bridge.acceptPage(page);if(page.length===0)break}
const result=bridge.finish();await s.query('CLOSE mm_publisher44');cursor=false
return {catalogSha256:sha(canonical({values:result.values.snapshotSha256,catalogue:result.catalogue})),definitionSha256:result.catalogue.definitionSha256,manifestSha256:result.catalogue.manifestSha256,leafCount:result.catalogue.leafRelations}
@@ -3,6 +3,7 @@ import {BUILD44_PROVENANCE,FIXED44_ROLE_TEMPLATE} from './fixed44-build-definiti
import {BUILD56_PROVENANCE,FIXED56_ROLE_TEMPLATE} from './fixed56-build-definition.js'
import {BUILD57_PROVENANCE} from './fixed57-build-definition.js'
import {BUILD58_PROVENANCE} from './fixed58-build-definition.js'
import {BUILD59_PROVENANCE,FIXED59_ROLE_TEMPLATE} from './fixed59-build-definition.js'
import {fixedCatalogueParentPeriodData} from './application-writer-profile-catalogue-parent-policy.js'
import {GROUPS,type CatalogueEdition,type Inventory} from './catalogue-projection.js'
export const EXPANDED44_POLICY_VERSION='source44-managed-leaves-3000-data-v1' as const
@@ -49,8 +50,24 @@ const EXPANDED58_DATA_POLICY = Object.freeze({
version: 'source58-managed-leaves-3000-data-v1',
definitionSha256: BUILD58_PROVENANCE.templateSha256,
})
const policies = {44: EXPANDED44_DATA_POLICY, 56: EXPANDED56_DATA_POLICY, 57: EXPANDED57_DATA_POLICY, 58: EXPANDED58_DATA_POLICY}
const relationCounts = {44: perRelation, 56: source56PerRelation, 57: source57PerRelation, 58: source57PerRelation}
const source59PerRelation = new Map(FIXED59_ROLE_TEMPLATE.tables.map(({relname}) => [String(relname), Object.fromEntries(
GROUPS.map(group => [group, FIXED59_ROLE_TEMPLATE[group].filter(row => (group === 'inheritance' ? row.child : row.relname) === relname).length]),
) as Record<typeof GROUPS[number],number>]))
const source59BaseRows = Object.freeze(Object.fromEntries(GROUPS.map(group => [group,FIXED59_ROLE_TEMPLATE[group].length])) as Record<typeof GROUPS[number],number>)
if (source59PerRelation.size !== 113 || parents.some(name => GROUPS.some(group => source59PerRelation.get(name)?.[group] !== source57PerRelation.get(name)?.[group]))) {
throw Error('Unexpected source59 IX topology')
}
const EXPANDED59_DATA_POLICY = Object.freeze({
...EXPANDED58_DATA_POLICY,
version: 'source59-managed-leaves-3000-data-v1',
baseRelations: source59PerRelation.size,
maxRelations: source59PerRelation.size + LEAVES,
baseRows: source59BaseRows,
maxRows: Object.freeze(Object.fromEntries(GROUPS.map(group => [group,source59BaseRows[group] + LEAVES * leafMax[group]])) as Record<typeof GROUPS[number],number>),
definitionSha256: BUILD59_PROVENANCE.templateSha256,
})
const policies = {44: EXPANDED44_DATA_POLICY, 56: EXPANDED56_DATA_POLICY, 57: EXPANDED57_DATA_POLICY, 58: EXPANDED58_DATA_POLICY, 59: EXPANDED59_DATA_POLICY}
const relationCounts = {44: perRelation, 56: source56PerRelation, 57: source57PerRelation, 58: source57PerRelation, 59: source59PerRelation}
export const catalogueTopologyPolicy = (edition: CatalogueEdition) => policies[edition]
export const expanded44ParentRowCountsData=()=>Object.freeze(Object.fromEntries(parents.map(name=>[name,Object.freeze({...perRelation.get(name)!})])))
export interface ManagedLeafData {readonly parent:string;readonly name:string}
@@ -0,0 +1,32 @@
import assert from "node:assert/strict"
import { test } from "node:test"
import { migrationManifestDigest } from "../../src/db/migration-manifest.js"
import { ixWriterProfileEnrollmentSql } from "../../src/db/internet-exchange-writer-profile.js"
import { approvedDefinition } from "./catalogue.mjs"
import { FIXED58_ROLE_TEMPLATE } from "./fixed58-build-definition.js"
import { BUILD59_PROVENANCE, FIXED59_ROLE_TEMPLATE } from "./fixed59-build-definition.js"
import { catalogueTopologyPolicy } from "./expanded44-data-policy.js"
test("source59 pins migration 0058 and alters only the IX catalogue and its existing servers references", () => {
assert.equal(BUILD59_PROVENANCE.manifestSha256, migrationManifestDigest())
assert.equal(catalogueTopologyPolicy(59).baseRelations, catalogueTopologyPolicy(58).baseRelations + 3)
const added = new Set(FIXED59_ROLE_TEMPLATE.tables.filter(row => String(row.relname).startsWith("ix_segment")).map(row => row.relname))
assert.deepEqual([...added].sort(), ["ix_segment_devices", "ix_segment_revisions", "ix_segments"])
for (const group of ["tables", "columns", "roleGrants", "inheritance", "indexes"]) {
assert.deepEqual(FIXED59_ROLE_TEMPLATE[group].filter(row => !added.has(row.relname)), FIXED58_ROLE_TEMPLATE[group])
}
assert.equal(FIXED59_ROLE_TEMPLATE.triggers.length - FIXED58_ROLE_TEMPLATE.triggers.length, 21)
assert.equal(FIXED59_ROLE_TEMPLATE.constraints.length - FIXED58_ROLE_TEMPLATE.constraints.length, 24)
})
test("source59 accepts the exact independently reviewed definition and restricts IX runtime grants", () => {
const approval = { format: "application-writer-source59-definition-approval-v1", provenance: BUILD59_PROVENANCE, template: FIXED59_ROLE_TEMPLATE }
assert.equal(approvedDefinition(approval).definitionSha256, BUILD59_PROVENANCE.templateSha256)
assert.throws(() => approvedDefinition({ ...approval, template: FIXED58_ROLE_TEMPLATE }))
const granted = FIXED59_ROLE_TEMPLATE.roleGrants.filter(row => row.rolname === "@mm-role:runtime@" && String(row.relname).startsWith("ix_"))
assert(granted.every(row => row.read === true && row.insert === true && row.truncate === false))
assert.deepEqual(granted.filter(row => row.update === true).map(row => row.relname), ["ix_segments"])
assert.deepEqual(granted.filter(row => row.delete === true).map(row => row.relname), ["ix_segment_devices"])
assert.match(ixWriterProfileEnrollmentSql("ix_runtime", "ix_capture"), /GRANT SELECT, INSERT ON TABLE public.ix_segment_revisions TO "ix_runtime"/)
assert.throws(() => ixWriterProfileEnrollmentSql('bad"role', "ix_capture"))
})
@@ -0,0 +1,25 @@
import { canonical, sha } from "./application-writer-profile-canonical.js"
import { GROUPS, sorted, type Inventory } from "./catalogue-projection.js"
import { FIXED58_ROLE_TEMPLATE } from "./fixed58-build-definition.js"
import { FIXED59_IX_DELTA } from "./fixed59-ix-delta.js"
import { IX_WRITER_PROFILE_DEFINITION_SHA256, IX_WRITER_PROFILE_MANIFEST_SHA256 } from "../../src/db/internet-exchange-writer-profile.js"
const prior: Inventory = {
...FIXED58_ROLE_TEMPLATE,
constraints: FIXED58_ROLE_TEMPLATE.constraints.filter(row => !(row.relname === "servers" && row.conname === "servers_type_check")),
}
export const FIXED59_ROLE_TEMPLATE: Readonly<Inventory> = Object.freeze({
...prior,
...Object.fromEntries(["tables", ...GROUPS].map(group => [group, sorted([...prior[group], ...FIXED59_IX_DELTA[group]])])),
})
export const BUILD59_PROVENANCE = Object.freeze({
format: "source59-ix-profile-v1",
manifestSha256: IX_WRITER_PROFILE_MANIFEST_SHA256,
templateSha256: IX_WRITER_PROFILE_DEFINITION_SHA256,
})
if (sha(canonical(FIXED59_ROLE_TEMPLATE)) !== BUILD59_PROVENANCE.templateSha256) {
throw new Error("Fixed59 writer catalogue changed")
}
@@ -0,0 +1,99 @@
// PostgreSQL 18.4 catalogue rows captured after migration 0058 and IX least-privilege enrollment.
import type { Inventory } from "./catalogue-projection.js"
export const FIXED59_IX_DELTA: Inventory = {
tables: [
{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=ar/@mm-role:operator@"],"owner":"@mm-role:operator@","relkind":"r","relname":"ix_segment_revisions","namespace":"public","reloptions":null,"partition_key":null,"relispartition":false,"relrowsecurity":false,"partition_bound":null,"relforcerowsecurity":false},
{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=ard/@mm-role:operator@"],"owner":"@mm-role:operator@","relkind":"r","relname":"ix_segment_devices","namespace":"public","reloptions":null,"partition_key":null,"relispartition":false,"relrowsecurity":false,"partition_bound":null,"relforcerowsecurity":false},
{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arw/@mm-role:operator@"],"owner":"@mm-role:operator@","relkind":"r","relname":"ix_segments","namespace":"public","reloptions":null,"partition_key":null,"relispartition":false,"relrowsecurity":false,"partition_bound":null,"relforcerowsecurity":false},
],
columns: [
{"acl":null,"type":"text","attnum":4,"attname":"actor_id","relname":"ix_segment_revisions","attnotnull":true,"attstorage":"x","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"integer","attnum":5,"attname":"applied_revision","relname":"ix_segments","attnotnull":false,"attstorage":"p","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"jsonb","attnum":3,"attname":"configuration","relname":"ix_segment_revisions","attnotnull":true,"attstorage":"x","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"jsonb","attnum":4,"attname":"configuration","relname":"ix_segments","attnotnull":true,"attstorage":"x","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"timestamp with time zone","attnum":5,"attname":"created_at","relname":"ix_segment_revisions","attnotnull":true,"attstorage":"p","attidentity":"","attgenerated":"","attcompression":"","default_expression":"now()"},
{"acl":null,"type":"uuid","attnum":1,"attname":"id","relname":"ix_segments","attnotnull":true,"attstorage":"p","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"text","attnum":2,"attname":"name","relname":"ix_segments","attnotnull":true,"attstorage":"x","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"integer","attnum":2,"attname":"revision","relname":"ix_segment_revisions","attnotnull":true,"attstorage":"p","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"integer","attnum":3,"attname":"revision","relname":"ix_segments","attnotnull":true,"attstorage":"p","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"uuid","attnum":1,"attname":"segment_id","relname":"ix_segment_devices","attnotnull":true,"attstorage":"p","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"uuid","attnum":1,"attname":"segment_id","relname":"ix_segment_revisions","attnotnull":true,"attstorage":"p","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"bigint","attnum":2,"attname":"server_id","relname":"ix_segment_devices","attnotnull":true,"attstorage":"p","attidentity":"","attgenerated":"","attcompression":"","default_expression":null},
{"acl":null,"type":"timestamp with time zone","attnum":6,"attname":"updated_at","relname":"ix_segments","attnotnull":true,"attstorage":"p","attidentity":"","attgenerated":"","attcompression":"","default_expression":"now()"},
],
triggers: [
{"tgname":"candidate_application_statement_guard","tgtype":30,"relname":"ix_segment_devices","tgnargs":0,"arg_bytes":0,"tgenabled":"A","definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.ix_segment_devices FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":false,"unconditional":true,"tginitdeferred":false,"function":"public.server_partition_writer_guard()","parent":false,"constraint":false},
{"tgname":"candidate_application_statement_guard","tgtype":30,"relname":"ix_segment_revisions","tgnargs":0,"arg_bytes":0,"tgenabled":"A","definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.ix_segment_revisions FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":false,"unconditional":true,"tginitdeferred":false,"function":"public.server_partition_writer_guard()","parent":false,"constraint":false},
{"tgname":"candidate_application_statement_guard","tgtype":30,"relname":"ix_segments","tgnargs":0,"arg_bytes":0,"tgenabled":"A","definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.ix_segments FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":false,"unconditional":true,"tginitdeferred":false,"function":"public.server_partition_writer_guard()","parent":false,"constraint":false},
{"tgname":"candidate_truncate_preadmitted","tgtype":34,"relname":"ix_segment_devices","tgnargs":0,"arg_bytes":0,"tgenabled":"A","definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.ix_segment_devices FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":false,"unconditional":true,"tginitdeferred":false,"function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"constraint":false},
{"tgname":"candidate_truncate_preadmitted","tgtype":34,"relname":"ix_segment_revisions","tgnargs":0,"arg_bytes":0,"tgenabled":"A","definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.ix_segment_revisions FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":false,"unconditional":true,"tginitdeferred":false,"function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"constraint":false},
{"tgname":"candidate_truncate_preadmitted","tgtype":34,"relname":"ix_segments","tgnargs":0,"arg_bytes":0,"tgenabled":"A","definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.ix_segments FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":false,"unconditional":true,"tginitdeferred":false,"function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"constraint":false},
{"tgname":"server_partition_writer_guard","tgtype":31,"relname":"ix_segment_devices","tgnargs":0,"arg_bytes":0,"tgenabled":"A","definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.ix_segment_devices FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":false,"unconditional":true,"tginitdeferred":false,"function":"public.server_partition_writer_guard()","parent":false,"constraint":false},
{"tgname":"server_partition_writer_guard","tgtype":31,"relname":"ix_segment_revisions","tgnargs":0,"arg_bytes":0,"tgenabled":"A","definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.ix_segment_revisions FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":false,"unconditional":true,"tginitdeferred":false,"function":"public.server_partition_writer_guard()","parent":false,"constraint":false},
{"tgname":"server_partition_writer_guard","tgtype":31,"relname":"ix_segments","tgnargs":0,"arg_bytes":0,"tgenabled":"A","definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.ix_segments FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":false,"unconditional":true,"tginitdeferred":false,"function":"public.server_partition_writer_guard()","parent":false,"constraint":false},
{"tgname":"RI_ConstraintTrigger_a_{oid}","tgtype":9,"relname":"ix_segments","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.ix_segments FROM ix_segment_devices NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1646","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_a_{oid}","tgtype":9,"relname":"ix_segments","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.ix_segments FROM ix_segment_revisions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1646","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_a_{oid}","tgtype":17,"relname":"ix_segments","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.ix_segments FROM ix_segment_devices NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1655","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_a_{oid}","tgtype":17,"relname":"ix_segments","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.ix_segments FROM ix_segment_revisions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1655","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_c_{oid}","tgtype":5,"relname":"ix_segment_devices","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.ix_segment_devices FROM ix_segments NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1644","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_c_{oid}","tgtype":5,"relname":"ix_segment_devices","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.ix_segment_devices FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1644","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_c_{oid}","tgtype":5,"relname":"ix_segment_revisions","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.ix_segment_revisions FROM ix_segments NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1644","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_c_{oid}","tgtype":17,"relname":"ix_segment_devices","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.ix_segment_devices FROM ix_segments NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1645","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_c_{oid}","tgtype":17,"relname":"ix_segment_devices","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.ix_segment_devices FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1645","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_c_{oid}","tgtype":17,"relname":"ix_segment_revisions","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.ix_segment_revisions FROM ix_segments NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1645","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_a_{oid}","tgtype":9,"relname":"servers","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM ix_segment_devices NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_restrict_del\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1648","parent":false,"constraint":true},
{"tgname":"RI_ConstraintTrigger_a_{oid}","tgtype":17,"relname":"servers","tgnargs":0,"arg_bytes":0,"tgenabled":"O","definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM ix_segment_devices NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","tgnewtable":null,"tgoldtable":null,"tgdeferrable":false,"tgisinternal":true,"unconditional":true,"tginitdeferred":false,"function":"pg_catalog#1655","parent":false,"constraint":true},
],
constraints: [
{"conname":"ix_segment_devices_pkey","contype":"p","relname":"ix_segment_devices","definition":"PRIMARY KEY (segment_id, server_id)","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_devices_segment_id_fkey","contype":"f","relname":"ix_segment_devices","definition":"FOREIGN KEY (segment_id) REFERENCES ix_segments(id) ON DELETE CASCADE","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_devices_segment_id_not_null","contype":"n","relname":"ix_segment_devices","definition":"NOT NULL segment_id","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_devices_server_id_fkey","contype":"f","relname":"ix_segment_devices","definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE RESTRICT","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_devices_server_id_not_null","contype":"n","relname":"ix_segment_devices","definition":"NOT NULL server_id","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_revisions_actor_id_not_null","contype":"n","relname":"ix_segment_revisions","definition":"NOT NULL actor_id","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_revisions_configuration_check","contype":"c","relname":"ix_segment_revisions","definition":"CHECK ((jsonb_typeof(configuration) = 'object'::text))","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_revisions_configuration_not_null","contype":"n","relname":"ix_segment_revisions","definition":"NOT NULL configuration","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_revisions_created_at_not_null","contype":"n","relname":"ix_segment_revisions","definition":"NOT NULL created_at","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_revisions_pkey","contype":"p","relname":"ix_segment_revisions","definition":"PRIMARY KEY (segment_id, revision)","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_revisions_revision_check","contype":"c","relname":"ix_segment_revisions","definition":"CHECK ((revision > 0))","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_revisions_revision_not_null","contype":"n","relname":"ix_segment_revisions","definition":"NOT NULL revision","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_revisions_segment_id_fkey","contype":"f","relname":"ix_segment_revisions","definition":"FOREIGN KEY (segment_id) REFERENCES ix_segments(id) ON DELETE CASCADE","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segment_revisions_segment_id_not_null","contype":"n","relname":"ix_segment_revisions","definition":"NOT NULL segment_id","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segments_check","contype":"c","relname":"ix_segments","definition":"CHECK (((applied_revision > 0) AND (applied_revision <= revision)))","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segments_configuration_check","contype":"c","relname":"ix_segments","definition":"CHECK ((jsonb_typeof(configuration) = 'object'::text))","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segments_configuration_not_null","contype":"n","relname":"ix_segments","definition":"NOT NULL configuration","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segments_id_not_null","contype":"n","relname":"ix_segments","definition":"NOT NULL id","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segments_name_key","contype":"u","relname":"ix_segments","definition":"UNIQUE (name)","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segments_name_not_null","contype":"n","relname":"ix_segments","definition":"NOT NULL name","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segments_pkey","contype":"p","relname":"ix_segments","definition":"PRIMARY KEY (id)","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segments_revision_check","contype":"c","relname":"ix_segments","definition":"CHECK ((revision > 0))","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segments_revision_not_null","contype":"n","relname":"ix_segments","definition":"NOT NULL revision","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"ix_segments_updated_at_not_null","contype":"n","relname":"ix_segments","definition":"NOT NULL updated_at","condeferred":false,"convalidated":true,"condeferrable":false},
{"conname":"servers_type_check","contype":"c","relname":"servers","definition":"CHECK ((type = ANY (ARRAY['jump-host'::text, 'exit-node'::text, 'home-router'::text, 'ix-node'::text])))","condeferred":false,"convalidated":true,"condeferrable":false},
],
indexes: [
{"relname":"ix_segment_devices","definition":"CREATE INDEX ix_segment_devices_server_idx ON public.ix_segment_devices USING btree (server_id)","index_name":"ix_segment_devices_server_idx","indisready":true,"indisvalid":true,"indisunique":false,"indisprimary":false},
{"relname":"ix_segment_devices","definition":"CREATE UNIQUE INDEX ix_segment_devices_pkey ON public.ix_segment_devices USING btree (segment_id, server_id)","index_name":"ix_segment_devices_pkey","indisready":true,"indisvalid":true,"indisunique":true,"indisprimary":true},
{"relname":"ix_segment_revisions","definition":"CREATE UNIQUE INDEX ix_segment_revisions_pkey ON public.ix_segment_revisions USING btree (segment_id, revision)","index_name":"ix_segment_revisions_pkey","indisready":true,"indisvalid":true,"indisunique":true,"indisprimary":true},
{"relname":"ix_segments","definition":"CREATE UNIQUE INDEX ix_segments_name_key ON public.ix_segments USING btree (name)","index_name":"ix_segments_name_key","indisready":true,"indisvalid":true,"indisunique":true,"indisprimary":false},
{"relname":"ix_segments","definition":"CREATE UNIQUE INDEX ix_segments_pkey ON public.ix_segments USING btree (id)","index_name":"ix_segments_pkey","indisready":true,"indisvalid":true,"indisunique":true,"indisprimary":true},
],
roleGrants: [
{"read":false,"delete":false,"insert":false,"update":false,"relname":"ix_segment_devices","rolname":"@mm-role:publisher@","trigger":false,"truncate":false},
{"read":false,"delete":false,"insert":false,"update":false,"relname":"ix_segment_revisions","rolname":"@mm-role:publisher@","trigger":false,"truncate":false},
{"read":false,"delete":false,"insert":false,"update":false,"relname":"ix_segments","rolname":"@mm-role:publisher@","trigger":false,"truncate":false},
{"read":true,"delete":false,"insert":false,"update":false,"relname":"ix_segment_devices","rolname":"@mm-role:capture@","trigger":false,"truncate":false},
{"read":true,"delete":false,"insert":false,"update":false,"relname":"ix_segment_revisions","rolname":"@mm-role:capture@","trigger":false,"truncate":false},
{"read":true,"delete":false,"insert":false,"update":false,"relname":"ix_segments","rolname":"@mm-role:capture@","trigger":false,"truncate":false},
{"read":true,"delete":false,"insert":true,"update":false,"relname":"ix_segment_revisions","rolname":"@mm-role:runtime@","trigger":false,"truncate":false},
{"read":true,"delete":false,"insert":true,"update":true,"relname":"ix_segments","rolname":"@mm-role:runtime@","trigger":false,"truncate":false},
{"read":true,"delete":true,"insert":true,"update":true,"relname":"ix_segment_devices","rolname":"@mm-role:operator@","trigger":true,"truncate":true},
{"read":true,"delete":true,"insert":true,"update":false,"relname":"ix_segment_devices","rolname":"@mm-role:runtime@","trigger":false,"truncate":false},
{"read":true,"delete":true,"insert":true,"update":true,"relname":"ix_segment_revisions","rolname":"@mm-role:operator@","trigger":true,"truncate":true},
{"read":true,"delete":true,"insert":true,"update":true,"relname":"ix_segments","rolname":"@mm-role:operator@","trigger":true,"truncate":true},
],
inheritance: [
],
}
@@ -12,8 +12,9 @@ const publicationFormat='application-writer-publication-approval-v1'
export const UPGRADE_APPROVAL_FORMAT='application-writer-upgrade44-to56-approval-v1'
export const UPGRADE57_APPROVAL_FORMAT='application-writer-upgrade56-to57-approval-v1'
export const UPGRADE58_APPROVAL_FORMAT='application-writer-upgrade57-to58-approval-v1'
export const UPGRADE59_APPROVAL_FORMAT='application-writer-upgrade58-to59-approval-v1'
export const REBIND58_APPROVAL_FORMAT='application-writer-rebind58-after-restore-approval-v1'
export const isUpgradeApprovalFormat=format=>[UPGRADE_APPROVAL_FORMAT,UPGRADE57_APPROVAL_FORMAT,UPGRADE58_APPROVAL_FORMAT,REBIND58_APPROVAL_FORMAT].includes(format)
export const isUpgradeApprovalFormat=format=>[UPGRADE_APPROVAL_FORMAT,UPGRADE57_APPROVAL_FORMAT,UPGRADE58_APPROVAL_FORMAT,UPGRADE59_APPROVAL_FORMAT,REBIND58_APPROVAL_FORMAT].includes(format)
function parsedApproval(value,format=publicationFormat){
const upgrade=isUpgradeApprovalFormat(format)
const keys=['format','manifestSha256','definitionSha256','catalogSha256','expectedHeadSha256','validityMs','expiresAt']
+10 -6
View File
@@ -3,11 +3,12 @@ import pg from 'pg'
import {createPrivateKey,createPublicKey,sign,X509Certificate} from 'node:crypto'
import {open,lstat,realpath,unlink} from 'node:fs/promises'
import {readProtected,writeFile,withJournalLease} from './operator-files.mjs'
import {retainPublisherTlsPeer,upgradeApproval,isUpgradeApprovalFormat,UPGRADE_APPROVAL_FORMAT,UPGRADE57_APPROVAL_FORMAT,UPGRADE58_APPROVAL_FORMAT,REBIND58_APPROVAL_FORMAT} from './operator-contract.mjs'
import {retainPublisherTlsPeer,upgradeApproval,isUpgradeApprovalFormat,UPGRADE_APPROVAL_FORMAT,UPGRADE57_APPROVAL_FORMAT,UPGRADE58_APPROVAL_FORMAT,UPGRADE59_APPROVAL_FORMAT,REBIND58_APPROVAL_FORMAT} from './operator-contract.mjs'
import {BUILD44_PROVENANCE} from './fixed44-build-definition.js'
import {BUILD56_PROVENANCE} from './fixed56-build-definition.js'
import {BUILD57_PROVENANCE} from './fixed57-build-definition.js'
import {BUILD58_PROVENANCE} from './fixed58-build-definition.js'
import {BUILD59_PROVENANCE} from './fixed59-build-definition.js'
import path from 'node:path'
import {captureDatabaseSourceObservation,assertDatabaseSourceObservationCurrent} from '../../src/db/database-source-observation.js'
import {MIGRATION_MANIFEST,migrationManifestDigest} from '../../src/db/migration-manifest.js'
@@ -19,6 +20,7 @@ const upgradeTransitions = {
[UPGRADE_APPROVAL_FORMAT]: { predecessor: BUILD44_PROVENANCE, successor: BUILD56_PROVENANCE, oldEdition: 44, newEdition: 56 },
[UPGRADE57_APPROVAL_FORMAT]: { predecessor: BUILD56_PROVENANCE, successor: BUILD57_PROVENANCE, oldEdition: 56, newEdition: 57 },
[UPGRADE58_APPROVAL_FORMAT]: { predecessor: BUILD57_PROVENANCE, successor: BUILD58_PROVENANCE, oldEdition: 57, newEdition: 58 },
[UPGRADE59_APPROVAL_FORMAT]: { predecessor: BUILD58_PROVENANCE, successor: BUILD59_PROVENANCE, oldEdition: 58, newEdition: 59 },
[REBIND58_APPROVAL_FORMAT]: { predecessor: BUILD58_PROVENANCE, successor: BUILD58_PROVENANCE, oldEdition: 58, newEdition: 58 },
}
export function requireCurrentCatalogueDefinition(approval,definition){
@@ -133,7 +135,7 @@ async function session(pool,deadlineAt,work){
return value
}
async function observeCurrentCatalogue(s,manifestSha256){
const security=await protectedStructure(s,installation,definition?.edition===58),catalogue=await collectFixedCatalogue(s)
const security=await protectedStructure(s,installation,(definition?.edition??44)>=58),catalogue=await collectFixedCatalogue(s)
if(catalogue.manifestSha256!==manifestSha256||catalogue.definitionSha256!==definition.definitionSha256)throw Error('Catalogue evidence differs from the approved migration manifest/definition')
// Inspect one extra row so an unexpected marker cannot hide after the expected prefix.
const expected=MIGRATION_MANIFEST.map(pin=>pin.file.slice(0,-4))
@@ -209,7 +211,7 @@ async function inspectPublication(pool,proposal,{deadlineAt=Date.now()+120000}={
if(!proposal||proposal.installationId!==installation.installationId||!(/^[a-f0-9]{64}$/).test(proposal.headSha256)||proposal.previousHeadSha256!==null&&!(/^[a-f0-9]{64}$/).test(proposal.previousHeadSha256))throw Error('Bounded public proposal required')
return session(pool,deadlineAt,async s=>{
s.phase('publication-inspection');await s.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');s.begun(true)
const first=await ownedContext(s,installation,PUBLISHER,'publisher-read');await protectedStructure(s,installation,definition?.edition===58)
const first=await ownedContext(s,installation,PUBLISHER,'publisher-read');await protectedStructure(s,installation,(definition?.edition??44)>=58)
if(canonical(sourceCoordinates(first))!==canonical(installation.expectedSource))throw Error('Pinned inspection source mismatch')
const facade=Object.freeze({query:(sql,values)=>s.query(sql,values)}),options={deadlineAt}
const observation=await captureDatabaseSourceObservation(facade,options);await sourceVerifier.verify(facade,observation,options)
@@ -251,7 +253,8 @@ async function preflightCurrentProfile(pool,{deadlineAt=Date.now()+120000}={}){
}
async function inspectCurrentCatalogue(pool,{deadlineAt=Date.now()+120000}={}){
const manifestSha256=migrationManifestDigest()
if(definition?.edition!==58||definition.manifestSha256!==manifestSha256||definition.definitionSha256!==BUILD58_PROVENANCE.templateSha256)throw Error('Approved source58 definition required')
const expected=definition?.edition===59?BUILD59_PROVENANCE:definition?.edition===58?BUILD58_PROVENANCE:undefined
if(!expected||definition.manifestSha256!==manifestSha256||definition.definitionSha256!==expected.templateSha256)throw Error('Approved current writer definition required')
return session(pool,deadlineAt,async s=>{
s.phase('source-catalogue-inspection');await s.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');s.begun(true)
const first=await ownedContext(s,installation,PUBLISHER,'publisher-read')
@@ -261,9 +264,10 @@ async function inspectCurrentCatalogue(pool,{deadlineAt=Date.now()+120000}={}){
const initial=await sourceVerifier.verify(facade,observation,options)
const {security,catalogSha256}=await observeCurrentCatalogue(s,manifestSha256)
const current=await readCurrent(s,installation,{validNow:false})
if(!current||current.policy.definitionSha256!==definition.definitionSha256||current.policy.manifestSha256!==manifestSha256||
const allowedHeads=definition.edition===59?[BUILD58_PROVENANCE,BUILD59_PROVENANCE]:[BUILD58_PROVENANCE]
if(!current||!allowedHeads.some(head=>head.templateSha256===current.policy.definitionSha256&&head.manifestSha256===current.policy.manifestSha256)||
canonical(current.policy.source)!==canonical(sourceCoordinates(first))||canonical(current.policy.roles)!==canonical(security.roles)){
throw Error('Approved source58 predecessor required')
throw Error('Approved same-installation writer predecessor required')
}
const terminal=await sourceVerifier.verify(facade,observation,options)
if(initial.platformProtectionVerified!==true||terminal.platformProtectionVerified!==true)throw Error('Protected source evidence required')
@@ -8,8 +8,9 @@ const digest = (value: string) => createHash("sha256").update(value).digest("hex
export const REVIEWED_PREDECESSORS = [
{ length: 44, manifestSha256: "fffae3381732e6f276ba276aca7185f6a87587edf8272ba42763b7f49bc59ef5" },
{ length: 56, manifestSha256: "b371d8d261f7dd69cf0e2ebc48d4316e3ad804360046bbaaf20e715da8812521" },
{ length: 57, manifestSha256: "7dbd678e59740c14a29594180c86b85b3b2dd53fd5aa7a72da6d14a513760dbc" },
] as const
export const REVIEWED_SUCCESSOR_MANIFEST = "7dbd678e59740c14a29594180c86b85b3b2dd53fd5aa7a72da6d14a513760dbc"
export const REVIEWED_SUCCESSOR_MANIFEST = "52c49786b6204dc11577131670cc0098c165ef54d419c25f0c70759e435c99fb"
export const canonical = (value: unknown): string => JSON.stringify(value, (_key, item: unknown) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item)
export async function observation(intent: DatabaseSchemaIntent, pin: MigrationReview, existingKey = false) {
// Fixed, purpose-separated server-keyed ACK issuer. Caller-supplied hashes
+13 -3
View File
@@ -185,6 +185,7 @@ test("known completed prefix resumes after fresh ownership without replaying ear
const reviewed44 = "fffae3381732e6f276ba276aca7185f6a87587edf8272ba42763b7f49bc59ef5"
const reviewed56 = "b371d8d261f7dd69cf0e2ebc48d4316e3ad804360046bbaaf20e715da8812521"
const reviewed57 = "7dbd678e59740c14a29594180c86b85b3b2dd53fd5aa7a72da6d14a513760dbc"
test("reviewed 44 migration history keeps its original keyed receipts and resumes only the current tail", async () => {
const f = fixture()
f.seedCompletedHistory(46, index => index < 44 ? reviewed44 : manifest.migrationManifestDigest())
@@ -199,13 +200,13 @@ test("reviewed 44 migration history keeps its original keyed receipts and resume
assert(f.events.every(event => !event.startsWith("file:")))
})
test("complete 56 receipts with or without the inherited 44 prefix dispatch only index 0057", async () => {
test("complete 56 receipts with or without the inherited 44 prefix dispatch the remaining two files", async () => {
for (const mixed of [false, true]) {
const f = fixture()
f.seedCompletedHistory(56, index => mixed && index < 44 ? reviewed44 : reviewed56)
const previous = structuredClone([...f.history.values()])
assert.equal((await f.run()).completedFiles, pins.length)
assert.deepEqual(f.events.filter(event => event.startsWith("file:")), ["file:0057_mcp_routing_charge_audit_index.sql"])
assert.deepEqual(f.events.filter(event => event.startsWith("file:")), ["file:0057_mcp_routing_charge_audit_index.sql", "file:0058_internet_exchange.sql"])
assert.deepEqual([...f.history.values()].slice(0, 56), previous)
f.events.length = 0
assert.equal((await f.run()).completedFiles, pins.length)
@@ -213,6 +214,15 @@ test("complete 56 receipts with or without the inherited 44 prefix dispatch only
}
})
test("the source58 migration history needs only the IX migration", async () => {
const f = fixture()
f.seedCompletedHistory(57, index => index < 44 ? reviewed44 : index < 56 ? reviewed56 : reviewed57)
const previous = structuredClone([...f.history.values()])
assert.equal((await f.run()).completedFiles, pins.length)
assert.deepEqual(f.events.filter(event => event.startsWith("file:")), ["file:0058_internet_exchange.sql"])
assert.deepEqual([...f.history.values()].slice(0, 57), previous)
})
test("history 56 cannot omit a receipt or interleave another manifest", async () => {
for (const invalid of ["incomplete", "interleaved"] as const) {
const f = fixture()
@@ -243,7 +253,7 @@ test("reviewed marker guards are rechecked after each file and a change stops th
}
const reviewed = prepare()
assert.equal((await reviewed.run()).completedFiles, pins.length)
assert.equal(reviewed.events.filter(event => event === "reviewed marker guards").length, 15)
assert.equal(reviewed.events.filter(event => event === "reviewed marker guards").length, pins.length - 44 + 2)
const damaged = prepare()
damaged.faults.guardDrift = true
await assert.rejects(damaged.run())
@@ -2,10 +2,10 @@ import assert from "node:assert/strict"
import { HEAD_DOMAIN, POLICY_DOMAIN, sha } from "./application-writer-profile-format.js"
import { stagedModelClient, syntheticFixture } from "./application-writer-profile-registry-read-model.test.js"
import { approvedRuntimeWriterProfile } from "./database-runtime-writer-profile.js"
import { RESTORE_WRITER_PROFILE_DEFINITION_SHA256, RESTORE_WRITER_PROFILE_MANIFEST_SHA256 } from "./database-restore-quarantine-profile.js"
import { IX_WRITER_PROFILE_DEFINITION_SHA256, IX_WRITER_PROFILE_MANIFEST_SHA256 } from "./internet-exchange-writer-profile.js"
const { subject, policy: basePolicy, state, signBody } = syntheticFixture("1")
const policy = { ...basePolicy, definitionSha256: RESTORE_WRITER_PROFILE_DEFINITION_SHA256, manifestSha256: RESTORE_WRITER_PROFILE_MANIFEST_SHA256 }
const policy = { ...basePolicy, definitionSha256: IX_WRITER_PROFILE_DEFINITION_SHA256, manifestSha256: IX_WRITER_PROFILE_MANIFEST_SHA256 }
const expected = { installationId: subject.installationId, source: policy.source, roles: policy.roles }
function publish(next: typeof policy, previous = state.latest[0]) {
@@ -1,7 +1,7 @@
import { z } from "zod"
import { canonical, epoch, publicKey, type ApplicationWriterPolicy } from "./application-writer-profile-format.js"
import { readCurrentApplicationWriterProfile, type ApplicationWriterRegistryReadSql } from "./application-writer-profile-registry-read.js"
import { RESTORE_WRITER_PROFILE_DEFINITION_SHA256, RESTORE_WRITER_PROFILE_MANIFEST_SHA256 } from "./database-restore-quarantine-profile.js"
import { IX_WRITER_PROFILE_DEFINITION_SHA256, IX_WRITER_PROFILE_MANIFEST_SHA256 } from "./internet-exchange-writer-profile.js"
const digest = z.string().regex(/^[a-f0-9]{64}$/)
const approvalSchema = z.object({
@@ -26,8 +26,8 @@ export function approvedRuntimeWriterProfile(
async check(client: ApplicationWriterRegistryReadSql) {
const current = await readCurrentApplicationWriterProfile(client, subject)
const policy = current?.policy
if (!current || !policy || policy.definitionSha256 !== RESTORE_WRITER_PROFILE_DEFINITION_SHA256 ||
policy.manifestSha256 !== RESTORE_WRITER_PROFILE_MANIFEST_SHA256 ||
if (!current || !policy || policy.definitionSha256 !== IX_WRITER_PROFILE_DEFINITION_SHA256 ||
policy.manifestSha256 !== IX_WRITER_PROFILE_MANIFEST_SHA256 ||
canonical(policy.source) !== canonical(expected.source) || canonical(policy.roles) !== canonical(expected.roles)) {
throw new Error("Текущий подписанный профиль записи PostgreSQL не подтверждён")
}
@@ -0,0 +1,33 @@
export const IX_WRITER_PROFILE_MANIFEST_SHA256 = "52c49786b6204dc11577131670cc0098c165ef54d419c25f0c70759e435c99fb"
export const IX_WRITER_PROFILE_DEFINITION_SHA256 = "8f6bcf73d3304879e023beeccc11cd2fa443aa4dd43b913d6f30419267ca64f3"
const rights = {
ix_segments: "SELECT, INSERT, UPDATE",
ix_segment_devices: "SELECT, INSERT, DELETE",
ix_segment_revisions: "SELECT, INSERT",
} as const
function identifier(name: string) {
if (!/^[a-z_][a-z0-9_]{0,62}$/.test(name)) throw new Error("Invalid IX profile role")
return `"${name}"`
}
/** Execute offline as application operator, within a retained preadmitted DDL transaction. */
export function ixWriterProfileEnrollmentSql(runtimeRole: string, captureRole: string) {
if (runtimeRole === captureRole) throw new Error("IX profile roles must be distinct")
const runtime = identifier(runtimeRole), capture = identifier(captureRole)
return Object.entries(rights).flatMap(([name, permissions]) => {
const table = "public." + name
return [
`REVOKE ALL ON TABLE ${table} FROM PUBLIC, ${runtime}, ${capture}`,
`GRANT ${permissions} ON TABLE ${table} TO ${runtime}`,
`GRANT SELECT ON TABLE ${table} TO ${capture}`,
`CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR UPDATE OR DELETE ON ${table} FOR EACH ROW EXECUTE FUNCTION public.server_partition_writer_guard()`,
`ALTER TABLE ${table} ENABLE ALWAYS TRIGGER server_partition_writer_guard`,
`CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR UPDATE OR DELETE ON ${table} FOR EACH STATEMENT EXECUTE FUNCTION public.server_partition_writer_guard()`,
`ALTER TABLE ${table} ENABLE ALWAYS TRIGGER candidate_application_statement_guard`,
`CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON ${table} FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()`,
`ALTER TABLE ${table} ENABLE ALWAYS TRIGGER candidate_truncate_preadmitted`,
]
}).join(";\n") + ";\n"
}
+5 -5
View File
@@ -12,10 +12,10 @@ const reviewed = (source: string, extra: Partial<MigrationReview> = {}) => {
const bytes = Buffer.from(source), file = "9000_fixture.sql"
return () => analyzeMigration(file, bytes, { file, sha256: createHash("sha256").update(bytes).digest("hex"), transaction: "owner-transaction", selfMarker: false, opaqueBodies: 0, bodyReview: [], effects: ["Explicit inert fixture review"], ...extra })
}
await test("all 57 exact file pins and every top-level schema operation are inventoried", () => {
assert.equal(inventory.length, 57); assert.equal(MIGRATION_MANIFEST.length, 57)
await test("all 58 exact file pins and every top-level schema operation are inventoried", () => {
assert.equal(inventory.length, 58); assert.equal(MIGRATION_MANIFEST.length, 58)
assert.equal(inventory.filter(file => file.transaction === "owner-transaction").length, 17)
assert.equal(inventory.filter(file => file.transaction === "self-managed-single-transaction").length, 40)
assert.equal(inventory.filter(file => file.transaction === "self-managed-single-transaction").length, 41)
assert(inventory.every(file => file.operations.length === file.statementCount && file.effects.length > 0))
let tables = new Set<string>()
for (const file of inventory) {
@@ -32,8 +32,8 @@ await test("DO BEGIN and stored function BEGIN are never transaction boundaries"
assert.equal(endpoint.topLevelTransactionControls.length, 2); assert.equal(endpoint.opaqueBodies.length, 2)
assert.deepEqual(endpoint.opaqueBodies.map(body => body.kind), ["stored-function-body", "executed-do-body"])
})
await test("0035 through 0057 have exact atomic file-owned terminal markers", () => {
assert.deepEqual(inventory.filter(file => file.selfMarker).map(file => file.id), ["0035_server_probe_journal", "0036_diagnostic_cancellations", "0037_server_probe_storage_v2", "0038_server_poll_journal_and_writer_guards", "0039_server_poll_storage", "0040_mcp_routing_reservations", "0041_server_poll_terminal_archives", "0042_server_poll_settings_epoch", "0043_mcp_routing_forward_charges", "0044_mcp_routing_manual_inverse_charges", "0045_mcp_operation_replay_tombstones", "0046_server_poll_operation_retirement", "0047_server_poll_retirement_claims", "0048_mcp_ordinary_retirement_claims", "0049_mcp_ordinary_telemetry_retirement", "0050_server_probe_retired_sources", "0051_server_probe_retirement_claims", "0052_server_probe_retired_gc", "0053_server_probe_terminal_proofs", "0054_mcp_geoip_retirement", "0055_mcp_backup_schedule_retirement", "0056_mcp_manual_inverse_recovery_v8", "0057_mcp_routing_charge_audit_index"])
await test("0035 through 0058 have exact atomic file-owned terminal markers", () => {
assert.deepEqual(inventory.filter(file => file.selfMarker).map(file => file.id), ["0035_server_probe_journal", "0036_diagnostic_cancellations", "0037_server_probe_storage_v2", "0038_server_poll_journal_and_writer_guards", "0039_server_poll_storage", "0040_mcp_routing_reservations", "0041_server_poll_terminal_archives", "0042_server_poll_settings_epoch", "0043_mcp_routing_forward_charges", "0044_mcp_routing_manual_inverse_charges", "0045_mcp_operation_replay_tombstones", "0046_server_poll_operation_retirement", "0047_server_poll_retirement_claims", "0048_mcp_ordinary_retirement_claims", "0049_mcp_ordinary_telemetry_retirement", "0050_server_probe_retired_sources", "0051_server_probe_retirement_claims", "0052_server_probe_retired_gc", "0053_server_probe_terminal_proofs", "0054_mcp_geoip_retirement", "0055_mcp_backup_schedule_retirement", "0056_mcp_manual_inverse_recovery_v8", "0057_mcp_routing_charge_audit_index", "0058_internet_exchange"])
for (const file of inventory.filter(file => file.selfMarker)) { assert.equal(file.selfMarker!.atomicWithAllFileEffects, true); assert.equal(file.selfMarker!.statement, file.statementCount - 2); assert.equal(file.currentOuterMarkerGap, false) }
assert.equal(inventory.filter(file => file.currentOuterMarkerGap).length, 34)
})
+13 -1
View File
@@ -879,6 +879,18 @@ const classifiedMigrationPins: readonly MigrationReview[] = [
"effects": [
"Adds the partial routing charge audit lookup index. Retains all rows and permissions."
]
},
{
"file": "0058_internet_exchange.sql",
"sha256": "75e2842f8d45909c37d1387bdfadf659606e6227b5542289e8c1d98269f5a0cd",
"transaction": "self-managed-single-transaction",
"selfMarker": true,
"opaqueBodies": 0,
"bodyReview": [],
"effects": [
"Allows the IX Node server type without changing existing rows.",
"Adds versioned IX configuration and device references."
]
}
]
@@ -901,7 +913,7 @@ export const migrationManifestDigest = () => hash(JSON.stringify(MIGRATION_MANIF
// resetting the accumulated public table set at the reviewed destructive0002.
// This is a necessary presence check, not a substitute for exact live column,
// constraint/index/function/trigger comparison by the control protocol.
export const CURRENT_REQUIRED_TABLES: readonly string[] = Object.freeze(["acme_settings","alert_engine_confirm_pending","alert_engine_cursor","alert_engine_prev_live","alert_engine_state","alert_groups","alert_history","alert_outbox","alert_rule_conditions","alert_rule_targets","alert_rules","alert_telegram_settings","app_users","backup_entries","backup_schedule_settings","backup_storage_settings","certificate_issue_jobs","certificate_renew_settings","config_revisions","data_migration","events","evobgp_settings","filter_rules","flow_asn_meta","flow_buckets","flow_daily_dims","flow_daily_facts","flow_hour_facts","flow_ip_meta","flow_minute_dims","flow_minute_stats","flow_settings_intents","flow_write_receipts","geoip_settings","internet_path_settings","internet_path_snapshots","mcp_artifacts","mcp_audit","mcp_budget_buckets","mcp_connections","mcp_credentials","mcp_diagnostic_cancellations","mcp_drift","mcp_grants","mcp_management_channels","mcp_oauth_clients","mcp_oauth_requests","mcp_operation_checkpoints","mcp_operation_replay_tombstones","mcp_operation_steps","mcp_operations","mcp_ordinary_retirement_claims","mcp_owner_revocations","mcp_path_checks","mcp_plans","mcp_portal_events","mcp_protections","mcp_read_leases","mcp_recovery_intents","mcp_requests","mcp_routing_forward_charges","mcp_routing_manual_inverse_charges","mcp_routing_observations","mcp_routing_profiles","mcp_routing_reservation_slots","mcp_routing_reservations","mcp_scoped_recovery_intents","mcp_secrets","mcp_settings","mcp_unauthorized_buckets","recursive_routes","router_log_collection_state","router_log_entries","router_log_settings","scheduler_runs","schema_migrations","server_poll_acks","server_poll_archives","server_poll_executions","server_poll_intents","server_poll_quarantines","server_poll_receipts","server_poll_retirement_claims","server_poll_storage_admissions","server_poll_storage_usage","server_probe_execution_payloads","server_probe_execution_quarantines","server_probe_execution_stages","server_probe_executions","server_probe_retired_sources","server_probe_retirement_claims","server_probe_storage_admissions","server_probe_storage_usage","server_probe_terminal_proofs","server_snapshots","servers","servers_api_ping_settings","servers_rest_ping_samples","traffic_collection_state","traffic_flow_settings","traffic_samples","traffic_settings","uptime_probe_samples","uptime_probes","uptime_resource_samples","uptime_settings","uptime_speed_probes","uptime_speed_test_runs","user_interface_bindings","vrf_production_tickets"])
export const CURRENT_REQUIRED_TABLES: readonly string[] = Object.freeze(["acme_settings","alert_engine_confirm_pending","alert_engine_cursor","alert_engine_prev_live","alert_engine_state","alert_groups","alert_history","alert_outbox","alert_rule_conditions","alert_rule_targets","alert_rules","alert_telegram_settings","app_users","backup_entries","backup_schedule_settings","backup_storage_settings","certificate_issue_jobs","certificate_renew_settings","config_revisions","data_migration","events","evobgp_settings","filter_rules","flow_asn_meta","flow_buckets","flow_daily_dims","flow_daily_facts","flow_hour_facts","flow_ip_meta","flow_minute_dims","flow_minute_stats","flow_settings_intents","flow_write_receipts","geoip_settings","internet_path_settings","internet_path_snapshots","ix_segment_devices","ix_segment_revisions","ix_segments","mcp_artifacts","mcp_audit","mcp_budget_buckets","mcp_connections","mcp_credentials","mcp_diagnostic_cancellations","mcp_drift","mcp_grants","mcp_management_channels","mcp_oauth_clients","mcp_oauth_requests","mcp_operation_checkpoints","mcp_operation_replay_tombstones","mcp_operation_steps","mcp_operations","mcp_ordinary_retirement_claims","mcp_owner_revocations","mcp_path_checks","mcp_plans","mcp_portal_events","mcp_protections","mcp_read_leases","mcp_recovery_intents","mcp_requests","mcp_routing_forward_charges","mcp_routing_manual_inverse_charges","mcp_routing_observations","mcp_routing_profiles","mcp_routing_reservation_slots","mcp_routing_reservations","mcp_scoped_recovery_intents","mcp_secrets","mcp_settings","mcp_unauthorized_buckets","recursive_routes","router_log_collection_state","router_log_entries","router_log_settings","scheduler_runs","schema_migrations","server_poll_acks","server_poll_archives","server_poll_executions","server_poll_intents","server_poll_quarantines","server_poll_receipts","server_poll_retirement_claims","server_poll_storage_admissions","server_poll_storage_usage","server_probe_execution_payloads","server_probe_execution_quarantines","server_probe_execution_stages","server_probe_executions","server_probe_retired_sources","server_probe_retirement_claims","server_probe_storage_admissions","server_probe_storage_usage","server_probe_terminal_proofs","server_snapshots","servers","servers_api_ping_settings","servers_rest_ping_samples","traffic_collection_state","traffic_flow_settings","traffic_samples","traffic_settings","uptime_probe_samples","uptime_probes","uptime_resource_samples","uptime_settings","uptime_speed_probes","uptime_speed_test_runs","user_interface_bindings","vrf_production_tickets"])
export interface LegacyBaselineEvidence {
inventoryComplete: boolean; databaseGeneration: string; unresolvedAttemptIds: readonly string[]
+1 -1
View File
@@ -63,7 +63,7 @@ export const servers = pgTable("servers", {
useSsl: boolean("use_ssl").notNull().default(true),
verifySsl: boolean("verify_ssl").notNull().default(false),
routingEndpointEpoch: uuid("routing_endpoint_epoch").notNull().defaultRandom(),
type: text("type", { enum: ["jump-host", "exit-node", "home-router"] })
type: text("type", { enum: ["jump-host", "exit-node", "home-router", "ix-node"] })
.notNull().default("home-router"),
site: text("site").notNull().default(""),
country: text("country").notNull().default(""),
+2
View File
@@ -39,6 +39,7 @@ import vxlanRoutes from "./routes/vxlan.js"
import containersRoutes from "./routes/containers.js"
import firewallRoutes from "./routes/firewall.js"
import greRoutes from "./routes/gre.js"
import ixRoutes from "./routes/internet-exchange.js"
import usersRoutes from "./routes/users.js"
import statisticsRoutes from "./routes/statistics.js"
import logsRoutes from "./routes/logs.js"
@@ -162,6 +163,7 @@ export async function buildApp(opts?: {
await app.register(containersRoutes, { prefix: "/api" })
await app.register(firewallRoutes, { prefix: "/api" })
await app.register(greRoutes, { prefix: "/api" })
await app.register(ixRoutes, { prefix: "/api" })
await app.register(usersRoutes, { prefix: "/api" })
await app.register(statisticsRoutes, { prefix: "/api" })
await app.register(logsRoutes, { prefix: "/api" })
+2
View File
@@ -186,6 +186,7 @@ const RULES: Rule[] = [
p.startsWith("/api/recursive") ||
p.startsWith("/api/probes") ||
p.startsWith("/api/internet-path") ||
p === "/api/ix" || p.startsWith("/api/ix/") ||
p.startsWith("/api/exec") ||
p.startsWith("/api/wireguard") ||
p.startsWith("/api/ipsec") ||
@@ -201,6 +202,7 @@ const RULES: Rule[] = [
p.startsWith("/api/recursive") ||
p.startsWith("/api/probes") ||
p.startsWith("/api/internet-path") ||
p === "/api/ix" || p.startsWith("/api/ix/") ||
p.startsWith("/api/exec") ||
p.startsWith("/api/wireguard") ||
p.startsWith("/api/ipsec") ||
@@ -12,6 +12,7 @@ export const ACCEPTANCE_SHARDS: Record<string, (actor: AuthUser) => Promise<unkn
acceptRouteCommunityContextMcp: async actor => (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),
+4
View File
@@ -76,6 +76,7 @@ import { prepareWireguardPeerDeletion, registerWireguardPeerDeleteActions } from
import { registerFirewallActions } from "./firewall-actions.js"
import { registerBfdMoveActions } from "./bfd-move-actions.js"
import { registerGreMutationActions } from "./gre-mutation-actions.js"
import { registerIxActions } from "./internet-exchange-actions.js"
import { registerGreRevisionActions } from "./gre-revision-actions.js"
import { prepareGreNativeChange, registerGreNativeServices } from "./gre-native-actions.js"
import { registerVpnCatalogActions } from "./vpn-catalog-actions.js"
@@ -91,6 +92,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"
@@ -700,6 +702,7 @@ registerWireguardInterfaceDeleteActions(add)
registerFirewallActions(add)
registerBfdMoveActions(add)
registerGreMutationActions(add)
registerIxActions(add)
registerGreRevisionActions(add)
registerGreNativeServices()
registerVpnCatalogActions(add)
@@ -710,6 +713,7 @@ registerTerminalDiagnosticActions(add)
registerEventsBatchActions(add)
registerGlobalReconciliationActions(add)
registerWireguardInterfaceUpdateActions(add)
registerInterfaceRenameActions(add)
registerWireguardImportActions(add)
registerServerConnectionActions(add)
registerIpsecPeerDeleteActions(add)
+8 -1
View File
@@ -8,9 +8,16 @@ const actual = await collectApplicationRoutes()
assert.equal(new Set(coverage.routes.map(routeKey)).size, coverage.routes.length, "Duplicate coverage route")
assert.deepEqual(coverage.routes.map(routeKey).sort(), actual.map(routeKey).sort(), "REST routes changed: update mcp-coverage.json with a typed tool, human-only reason, or explicit pending adapter")
const implemented = new Set(listActions().map(action => action.operation.id))
const ixInterfaceRoutes = new Set([
"GET /api/ix/nodes",
"GET /api/ix/segments/:id/linux",
"GET /api/ix/topology",
"POST /api/ix/preview",
"POST /api/ix/segments",
])
for (const route of coverage.routes) {
assert.equal(route.status === "mediated", route.operations.length > 0, routeKey(route))
for (const operation of route.operations) assert.ok(implemented.has(operation), `Unknown tool mapping: ${operation} for ${routeKey(route)}`)
if (route.status === "excluded") assert.match(route.path, /^(?:\/api\/(?:mcp(?:\/|$)|auth\/config$)|\/mcp$|\/oauth\/|\/\.well-known\/|\/_internal\/mcp\/|\/health$)/, "Business endpoint cannot be silently excluded")
if (route.status === "excluded" && !ixInterfaceRoutes.has(routeKey(route))) assert.match(route.path, /^(?:\/api\/(?:mcp(?:\/|$)|auth\/config$)|\/mcp$|\/oauth\/|\/\.well-known\/|\/_internal\/mcp\/|\/health$)/, "Business endpoint cannot be silently excluded")
}
console.log("MCP REST coverage:", JSON.stringify({ tools: listActions().length, routes: actual.length, mediated: coverage.routes.filter(route => route.status === "mediated").length, excluded: coverage.routes.filter(route => route.status === "excluded").length, pending: coverage.routes.filter(route => route.status === "pending").length }))
@@ -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))
@@ -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<string, Record<string, string>[]> = {
"/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<string, string> }[] = []
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<string, string> = 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<void>(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<void>((resolve, reject) => router.close(error => error ? reject(error) : resolve()))
}
}
@@ -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<typeof recordSchema>
type Row = Record<string, string>
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<string, Reference>; 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<ReturnType<typeof routerClient>>["client"], path: string): Promise<Row[]> {
const routes = path.endsWith("?static=true")
let raw: unknown
try { raw = await client.get<unknown>(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<string>, 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<unknown>) {
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<string, unknown>) => (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<string, string>)[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. Пользовательские скрипты и история метрик по имени не переписываются.", "При неясном исходе требуется сверка. Команда не повторяется автоматически."],
}
},
})
}
@@ -0,0 +1,114 @@
import { z } from "zod"
import { ixChangeSchema, type IxChange } from "@mmapp/contracts/internet-exchange"
import { pool } from "../../db/index.js"
import { applyIxChange, currentIxChange, ixTarget, observeIxSegment, prepareIxChange, reconcileIxChange } from "../../services/internet-exchange-execution.js"
import { IxConflict, ixDeviceIds } from "../../services/internet-exchange-plan.js"
import { listIxSegments, readIxRevision, readIxSegment } from "../../services/internet-exchange-store.js"
import type { add } from "./actions.js"
import type { ActionContext } from "./action-types.js"
import { McpError } from "./security.js"
import { registerMcpService } from "./service-registry.js"
export const ixOperation = "network.ix.configure"
export const ixRestoreOperation = "network.ix.restore"
const service = "network.ix.configure"
const id = z.string().uuid()
const revision = z.number().int().positive()
const changeArgs = z.object({ change: ixChangeSchema }).strict()
const restoreArgs = z.object({ id, revision, restoreRevision: revision }).strict()
const sealedInput = z.object({
id, change: ixChangeSchema, actorId: z.string().min(1),
before: z.object({ revision: z.number().int().nonnegative(), devices: z.string().regex(/^[a-f0-9]{64}$/) }).strict(),
}).strict()
async function present<T>(work: () => Promise<T>) {
try { return await work() } catch (error) {
if (error instanceof IxConflict) throw new McpError(error.statusCode === 400 ? "INVALID_INPUT" : error.statusCode === 404 ? "NOT_FOUND" : "STATE_CONFLICT", error.message, error.statusCode)
throw error
}
}
async function prepare(change: IxChange, context: ActionContext) {
const serverIds = await ixTarget(change)
if (serverIds.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Нет доступа ко всем устройствам сегмента")
const result = await prepareIxChange(change)
const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=ANY($1::text[]) LIMIT 1001", [serverIds.map(String)])
const objects = [...new Map(result.objects.concat(protections.rows.map(({ object_ref: object }) => ({
kind: object.kind, id: object.id, serverId: object.serverId,
}))).map(object => [JSON.stringify(object), object])).values()]
if (objects.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен лимит защищённых ресурсов плана IX", 409)
return {
target: { scope: "servers" as const, serverIds },
before: result.before, objects, snapshots: [],
steps: [{ kind: "service" as const, service, input: { id: result.id, change, before: result.before, actorId: context.connection.owner_id } }],
mutationCost: result.native.tasks.length + 1,
warnings: [
"Изменение затрагивает только выделенные интерфейсы и управляемые объекты IX. Проверка конфигурации не подтверждает прохождение трафика.",
...result.native.tasks.slice(0, 12).map(task => `Узел ${task.serverId} · ${task.path} · ${task.fields ? task.rosId ? "изменить" : "создать" : "удалить"}`),
],
}
}
async function restore(input: Record<string, unknown>, context: ActionContext) {
const args = restoreArgs.parse(input)
const current = await readIxSegment(args.id, context.transactionClient)
if (!current || ixDeviceIds(current.configuration).some(id => !context.connection.policy.scope.serverIds.includes(id))) {
throw new McpError("NOT_FOUND", "Сегмент IX не найден", 404)
}
return { id: args.id, revision: args.revision, configuration: await readIxRevision(args.id, args.restoreRevision) }
}
export function registerIxActions(register: typeof add) {
registerMcpService(service, {
current: input => present(() => {
const args = sealedInput.parse(input)
return currentIxChange(args.id, args.change)
}),
execute: input => present(() => {
const args = sealedInput.parse(input)
return applyIxChange(args.id, args.change, args.before, args.actorId)
}),
verify: async (input, result) => {
const args = sealedInput.parse(input)
const row = await readIxSegment(args.id)
return (result as { configurationVerified?: boolean })?.configurationVerified === true && row?.appliedRevision === args.change.revision + 1
},
reconcile: (input, authorize) => {
const args = sealedInput.parse(input)
return reconcileIxChange(args.id, args.change, args.before, authorize)
},
})
register("network.ix.list", "mm_ix_segments_list", "Сегменты обмена трафиком", "network", "mm:network:read", "read", "servers",
z.object({ limit: z.number().int().min(1).max(25).default(10), offset: z.number().int().nonnegative().max(1000000).default(0) }).strict(), {
read: async (input, context) => {
const items = await listIxSegments(Number(input.limit), Number(input.offset), context.connection.policy.scope.serverIds)
return { items: items.map(segment => ({
id: segment.id, name: segment.configuration.name, sites: segment.configuration.sites.map(site => site.serverId),
revision: segment.revision, appliedRevision: segment.appliedRevision, updatedAt: segment.updatedAt,
})) }
},
})
register("network.ix.read", "mm_ix_segment_get", "Конфигурация сегмента IX", "network", "mm:network:read", "read", "servers",
z.object({ id }).strict(), {
target: async (input, context) => {
const row = await readIxSegment(String(input.id), context.transactionClient)
if (!row) throw new McpError("NOT_FOUND", "Сегмент IX не найден", 404)
return { scope: "servers", serverIds: await ixTarget({ id: row.id, revision: row.revision, configuration: row.configuration }) }
},
read: async (input, context) => {
const row = await readIxSegment(String(input.id))
if (!row) throw new McpError("NOT_FOUND", "Сегмент IX не найден", 404)
const ids = await ixTarget({ id: row.id, revision: row.revision, configuration: row.configuration })
if (ids.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Нет доступа ко всем устройствам сегмента")
return { segment: row, observations: await observeIxSegment(row) }
},
})
register(ixOperation, "mm_ix_change_plan", "План изменения сегмента IX", "network", "mm:network:write", "write", "servers", changeArgs, {
target: async input => ({ scope: "servers", serverIds: await ixTarget(changeArgs.parse(input).change) }),
prepare: (input, context) => present(() => prepare(changeArgs.parse(input).change, context)),
})
register(ixRestoreOperation, "mm_ix_restore_plan", "План восстановления ревизии IX", "network", "mm:network:write", "write", "servers", restoreArgs, {
target: async (input, context) => ({ scope: "servers", serverIds: await ixTarget(await restore(input, context)) }),
prepare: (input, context) => present(async () => prepare(await restore(input, context), context)),
})
}
@@ -17,11 +17,15 @@ export async function acceptOspfRouteProofMcp(actor: AuthUser) {
const router = createServer(async (req, res) => {
res.setHeader("content-type", "application/json")
if (req.method === "GET" && req.url === tablePath) { reads++; res.end(JSON.stringify(tables)); return }
if (!["/rest/routing/route", "/rest/routing/route/get"].includes(req.url ?? "")) { res.statusCode = 404; res.end("{}"); return }
const url = new URL(req.url ?? "/", "http://fixture.invalid")
if (!["/rest/routing/route", "/rest/routing/route/get"].includes(url.pathname)) { res.statusCode = 404; res.end("{}"); return }
reads++
if (beforeReply) { const hook = beforeReply; beforeReply = undefined; await hook() }
if (nativeError) { res.statusCode = 400; res.end('{"message":"NEVER_ROUTE_PROVIDER_TOKEN"}'); return }
if (req.method === "GET") { res.end(JSON.stringify(rows)); return }
if (req.method === "GET") {
if (url.searchParams.get("dst-address") !== selection.prefix || url.searchParams.get("routing-table") !== selection.table) { res.statusCode = 400; res.end("{}"); return }
res.end(JSON.stringify(rows)); return
}
let body = ""; for await (const chunk of req) body += chunk
const key = JSON.parse(body)["value-name"]
if (key === "ospf.metric" && afterMetric) { const hook = afterMetric; afterMetric = undefined; await hook() }
@@ -36,7 +36,7 @@ export function registerOspfRouteProofActions(register: typeof add) {
if (error instanceof McpError) throw error
throw new McpError("STATE_UNAVAILABLE", input.expectAbsent === true
? "Отсутствие точного префикса во всей выбранной таблице не подтверждено"
: "Активный OSPF-маршрут, его метрика или выбранный выход не подтверждены; предел источника 10000 строк и 1 МиБ", 409)
: "Активный OSPF-маршрут, его метрика или выход через указанный интерфейс не подтверждены", 409)
}
},
})
+18
View File
@@ -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")
+9 -3
View File
@@ -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<McpPolicyPreviewRequest, "objects" | "protections"> {
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]),
@@ -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()
+3 -2
View File
@@ -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])
@@ -34,6 +34,10 @@ export function publicRoutingCommunityChanges(evidence: ChangeEvidence) {
/** Service recovery has the same private native bodies as its forward plan. */
export function publicMcpChangeEvidence(operationId: string, evidence: ChangeEvidence) {
if (operationId === "network.ix.configure" || operationId === "network.ix.restore") {
return { ...evidence, before: { revision: record(evidence.before).revision }, steps: evidence.steps.map(step => step.kind === "service"
? { ...step, input: { id: step.input.id, change: step.input.change } } : step) }
}
if (operationId === "routing.bfd.configuration.move") {
const before = evidence.before as { order?: string[] } | undefined
return { ...evidence, before: { order: before?.order }, steps: evidence.steps.map(step => step.kind === "service"
@@ -51,6 +51,7 @@ function fixture(service='routing.community') {
'./routing-profile-authority.js':{routingProfileAuthorityBindingSchema:{parse:value=>value}},
'./routing-community-scoped-request.js':{},'./routing-community-scoped-checkpoint.js':{},'./routing-community-scoped-recovery-source.js':{},
'./security.js':{fingerprint,redact,McpError,decryptSecretExisting:async value=>{decryptCalls++;return value;},privateFingerprinterExisting:async()=>{if(keyUnavailable)throw Error('key unavailable');return mac;}},
'./policy.js':{hasUnlimitedMcpRates:()=>assert.fail('Forward transport verification must not evaluate rate policy')},
'./store.js':{},'./routing-reservation-contract.js':{},'./routing-reservation-transaction.js':{},'./routing-reservation-runtime.js':{}
};
const verifier=load('routing-reservation-store.ts',providers).assertPersistentRoutingForwardTransportCharge;
@@ -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,
@@ -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])
+4 -4
View File
@@ -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"
@@ -64,6 +64,7 @@ export async function acceptServerProbeStorageHostSql() {
let raw = ""; for await (const chunk of request) raw += chunk
const body = JSON.parse(raw) as Record<string, unknown>
assert.equal(body.address, "127.0.0.1"); assert.equal(body.count, "1")
assert.equal(Object.hasOwn(body, "src-address"), false)
if (requests === 2) {
response.once("close", secondClosed)
secondValidated()
@@ -35,9 +35,11 @@ export async function acceptWireguardImport(actor: AuthUser) {
for (const hidden of [privateKey, peerOne, peerTwo, generated, content, "NEVER_IMPORT_"]) assert.equal(text.includes(hidden), false, "Public response must omit raw imported/native keys and configuration")
}
const router = createServer((req, res) => {
const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, ""))
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname)
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
res.setHeader("content-type", "application/json")
if (req.method === "GET") { reads++; if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }; res.end(JSON.stringify(rows[path])); return }
if (req.method === "GET") { reads++; if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }; res.end(JSON.stringify(selectedRows())); return }
if (!["PUT", "PATCH"].includes(req.method ?? "")) { res.statusCode = 400; res.end("{}"); return }
let raw = ""
req.on("data", chunk => { raw += chunk }); req.on("end", () => {
@@ -24,18 +24,23 @@ export async function acceptWireguardInterfaceCreation(actor: AuthUser) {
initial["/interface"] = [{ ".id": "*af", name: "wg-foreign", type: "wg" }]
initial["/ip/address"] = [{ ".id": "*vf", address: "203.0.113.2/24", interface: "ether1", dynamic: "false", disabled: "false" }]
initial["/ipv6/address"] = []
initial["/ip/route"] = Array.from({ length: 12_608 }, (_, index) => ({
".id": `*r${index.toString(16)}`, dynamic: "true", bgp: "true", gateway: "192.0.2.1", "immediate-gw": "192.0.2.1%ether1",
}))
let rows = structuredClone(initial), mode = "normal", reads = 0, commands: string[] = [], counter = 0
let afterWrite: (() => Promise<unknown>) | undefined
const reset = () => { rows = structuredClone(initial); mode = "normal"; commands = []; counter = 0; afterWrite = undefined }
const router = createServer((req, res) => {
res.setHeader("content-type", "application/json")
const path = decodeURIComponent((req.url ?? "").slice(5))
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname)
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
if (req.method === "GET") {
reads++
if (mode === "provider-error") { res.statusCode = 400; res.end('{"message":"NEVER_PROVIDER_SECRET"}'); return }
if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }
rows["/interface/wireguard/peers"][0].rx = String(reads)
res.end(JSON.stringify(rows[path])); return
res.end(JSON.stringify(selectedRows())); return
}
const base = ["/interface/wireguard/peers", "/interface/wireguard", "/ip/address", "/ipv6/address"].find(base => path === base || path.startsWith(base + "/"))
if (!base || !["PUT", "PATCH"].includes(req.method ?? "")) { res.statusCode = 400; res.end("{}"); return }
@@ -26,7 +26,9 @@ export async function acceptWireguardInterfaceDeletion(actor: AuthUser) {
let duringRead: (() => Promise<unknown>) | undefined
const router = createServer((req, res) => {
res.setHeader("content-type", "application/json")
const path = decodeURIComponent((req.url ?? "").slice(5))
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname)
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
if (req.method === "GET") {
gets++
const respond = () => {
@@ -34,7 +36,7 @@ export async function acceptWireguardInterfaceDeletion(actor: AuthUser) {
if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }
// Runtime traffic and DHCP lease clocks must not become configuration drift.
rows["/interface/wireguard/peers"].find(row => row[".id"] === "*pf")!.rx = String(gets)
res.end(JSON.stringify(rows[path]))
res.end(JSON.stringify(selectedRows()))
}
if (duringRead) { const hook = duringRead; duringRead = undefined; void hook().then(respond, () => { res.statusCode = 503; res.end("{}"); }); return }
respond(); return
@@ -89,7 +91,8 @@ export async function acceptWireguardInterfaceDeletion(actor: AuthUser) {
await assert.rejects(prepare(true), (error: { code?: string }) => error.code === "DEPENDENCY_CONFLICT")
assert.equal(commands.length, 0, "Native alias must not acquire cascade permission")
for (const [path, row] of [
["/interface/list/member", { interface: "wg-cascade", list: "LAN" }], ["/interface/vxlan", { interface: "wg-cascade" }], ["/ip/route", { gateway: "198.19.244.2%wg-cascade" }],
["/interface/list/member", { interface: "wg-cascade", list: "LAN" }], ["/interface/vxlan", { interface: "wg-cascade" }],
["/ip/route", { gateway: "wg-cascade" }], ["/ip/route", { dynamic: "true", "immediate-gw": "198.19.244.2%wg-cascade" }],
["/routing/filter/rule", { rule: 'set gw "wg-cascade";' }], ["/ip/firewall/filter", { "in-interface": "wg-cascade" }], ["/ip/dhcp-server", { interface: "wg-cascade" }],
["/routing/filter/rule", { rule: 'set gw "wg\\2dcascade";' }],
["/ip/ipsec/peer", { "local-address": "198.19.244.1" }], ["/routing/bgp/session", { dynamic: "true", "local.address": "2001:0db8:0244:0:0:0:0:1" }],
@@ -31,13 +31,15 @@ export async function acceptWireguardInterfaceUpdate(actor: AuthUser) {
const reset = () => { rows = structuredClone(initial); commands = []; mode = "normal"; afterWrite = undefined }
const router = createServer((req, res) => {
res.setHeader("content-type", "application/json")
const path = decodeURIComponent((req.url ?? "").slice(5))
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname)
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
if (req.method === "GET") {
reads++
if (mode === "provider-error") { res.statusCode = 500; res.end('{"message":"NEVER_PROVIDER_SECRET"}'); return }
if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }
rows["/interface/wireguard/peers"][0].rx = String(reads)
res.end(JSON.stringify(rows[path])); return
res.end(JSON.stringify(selectedRows())); return
}
const base = ["/interface/wireguard", "/ip/address", "/ipv6/address"].find(base => path === base || path.startsWith(base + "/"))
if (!base || !["PUT", "PATCH", "DELETE"].includes(req.method ?? "")) { res.statusCode = 400; res.end("{}"); return }
@@ -33,13 +33,15 @@ export async function acceptWireguardPeerCreation(actor: AuthUser) {
const reset = () => { rows = structuredClone(initial); mode = "normal"; commands = []; afterWrite = undefined }
const router = createServer((req, res) => {
res.setHeader("content-type", "application/json")
const path = decodeURIComponent((req.url ?? "").slice(5)), base = "/interface/wireguard/peers"
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname), base = "/interface/wireguard/peers"
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
if (req.method === "GET") {
reads++
if (mode === "provider-error" || mode === "dependency-error" && path === "/ip/address") { res.statusCode = 400; res.end('{"message":"NEVER_PROVIDER_SECRET"}'); return }
if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }
rows[base][0].rx = String(reads)
res.end(JSON.stringify(rows[path])); return
res.end(JSON.stringify(selectedRows())); return
}
if (!["PUT", "PATCH"].includes(req.method ?? "") || !(path === base || path === base + "/*c")) { res.statusCode = 400; res.end("{}"); return }
let raw = ""
@@ -32,8 +32,10 @@ export async function acceptWireguardRevisionRestore(actor:AuthUser){
const reset=()=>{rows=structuredClone(initial);commands=[];mode="normal";afterWrite=undefined}
const router=createServer(async(req,res)=>{
res.setHeader("content-type","application/json")
const path=decodeURIComponent((req.url??"").slice(5))
if(req.method==="GET"){reads++;if(!Object.hasOwn(rows,path)){res.statusCode=404;res.end("{}");return}res.end(JSON.stringify(rows[path]));return}
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname)
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
if(req.method==="GET"){reads++;if(!Object.hasOwn(rows,path)){res.statusCode=404;res.end("{}");return}res.end(JSON.stringify(selectedRows()));return}
let raw="";for await(const chunk of req)raw+=chunk
const body=raw?JSON.parse(raw)as Record<string,string>:{},base=["/interface/wireguard/peers","/interface/wireguard","/ip/address","/ipv6/address"].find(p=>path===p||path.startsWith(p+"/"))
if(!base||!["PUT","PATCH","DELETE"].includes(req.method??"")){res.statusCode=400;res.end("{}");return}
+70
View File
@@ -0,0 +1,70 @@
import { randomUUID } from "node:crypto"
import type { FastifyPluginAsync } from "fastify"
import { z } from "zod"
import { ixChangeSchema } from "@mmapp/contracts/internet-exchange"
import { withApplicationMutationLock } from "../services/config-mutation-lock.js"
import { observeIxSegment, readIxLinuxClient } from "../services/internet-exchange-execution.js"
import { compileIxConfiguration, IxConflict } from "../services/internet-exchange-plan.js"
import { assertIxRevision, listIxSegments, readIxSegment, writeIxRevision } from "../services/internet-exchange-store.js"
const paging = z.object({
limit: z.coerce.number().int().min(1).max(100).default(25),
offset: z.coerce.number().int().min(0).max(1000000).default(0),
}).strict()
const ixRoutes: FastifyPluginAsync = async app => {
app.setErrorHandler((error, request, reply) => {
if (error instanceof z.ZodError) return reply.code(400).send({ error: "Некорректные параметры IX", details: error.issues })
if (error instanceof IxConflict) return reply.code(error.statusCode).send({ error: error.message })
request.log.error({ error: error instanceof Error ? error.name : "unknown" }, "IX request failed")
return reply.code(503).send({ error: "Хранилище IX недоступно" })
})
app.get("/ix/segments", async request => {
const input = paging.parse(request.query)
return { items: await listIxSegments(input.limit, input.offset) }
})
app.get("/ix/nodes", async () => {
const { pool } = await import("../db/index.js")
const { rows } = await pool.query("SELECT id,name,site,country,enabled FROM servers WHERE type='ix-node' ORDER BY name,id LIMIT 500")
return { items: rows }
})
app.get("/ix/topology", async () => {
const segments = await listIxSegments(100, 0)
const links = segments.filter(segment => segment.appliedRevision === segment.revision).flatMap(({ configuration }) =>
configuration.sites.flatMap(site => site.interfaces.flatMap(binding => binding.peerServerId === undefined
? []
: [{ from: site.serverId, to: binding.peerServerId, segment: configuration.name }])))
return { links }
})
app.get("/ix/segments/:id/status", async request => {
const { id } = z.object({ id: z.string().uuid() }).parse(request.params)
const segment = await readIxSegment(id)
if (!segment) throw new IxConflict("Сегмент IX не найден", 404)
return { sites: await observeIxSegment(segment) }
})
app.get("/ix/segments/:id/linux", async request => {
const { id } = z.object({ id: z.string().uuid() }).parse(request.params)
const { serverId, interfaceName } = z.object({ serverId: z.coerce.number().positive().int().safe(), interfaceName: z.string().regex(/^[A-Za-z0-9_.-]{1,48}$/) }).strict().parse(request.query)
const segment = await readIxSegment(id)
if (!segment) throw new IxConflict("Сегмент IX не найден", 404)
return readIxLinuxClient(segment, serverId, interfaceName)
})
app.post("/ix/segments", async request => {
const input = ixChangeSchema.parse(request.body)
const id = input.id ?? randomUUID()
compileIxConfiguration(id, input.configuration)
return withApplicationMutationLock(async () => {
assertIxRevision(input.id ? await readIxSegment(id) : null, input.revision)
const segment = await writeIxRevision(id, input.revision, input.configuration, request.authUser?.id ?? "system", false)
return { segment }
})
})
app.post("/ix/preview", async request => {
const input = ixChangeSchema.parse(request.body)
const id = input.id ?? randomUUID()
assertIxRevision(input.id ? await readIxSegment(id) : null, input.revision)
return { id, objects: compileIxConfiguration(id, input.configuration) }
})
}
export default ixRoutes
+14
View File
@@ -19,6 +19,8 @@ import { accessGeneration, decryptSecret, encryptSecret, McpError, randomToken,
import { createMcpConnection, listMcpConnections, getMcpSettings, updateMcpSettings, getConnection, assertConnectionVisible, rotateConnectionToken, setConnectionStatus, updateMcpConnection, connectionWrite, requestAccountingWrite, audit } from "../modules/mcp/store.js"
import { approvePlan, loadPlan, publicPlan, readMcpPolicyPreviewInput } from "../modules/mcp/runtime.js"
import { applyPlan, invokeMcpTool } from "../modules/mcp/runtime.js"
import { ixChangeSchema } from "@mmapp/contracts/internet-exchange"
import { ixOperation, ixRestoreOperation } from "../modules/mcp/internet-exchange-actions.js"
import { findOwnerPlanReplayTombstone } from "../modules/mcp/operation-replay-tombstones.js"
import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing"
import { revalidateMcpOwner } from "../modules/mcp/store.js"
@@ -411,6 +413,18 @@ export function createMcpRoutes(checkClaims: typeof assertPortalClaimsCurrent =
await auditOwnerRequest(actor.id, "controller.web.plan", input.connectionId, { profileId: input.profileId ?? null })
return ownerDelegate(async () => invokeMcpTool(principal, "mm_route_profile_save_plan", payload))
})
app.post("/mcp/ix/plan", async request => {
const { connectionId, change } = z.object({ connectionId: uuid, change: ixChangeSchema }).strict().parse(request.body)
const actor = actorOf(request.authUser), principal = await ownerPrincipal(connectionId, actor, ixOperation)
await auditOwnerRequest(actor.id, "ix.web.plan", connectionId, { segmentId: change.id ?? null })
return ownerDelegate(() => invokeMcpTool(principal, "mm_ix_change_plan", { change }))
})
app.post("/mcp/ix/restore", async request => {
const { connectionId, ...input } = z.object({ connectionId: uuid, id: uuid, revision: z.number().int().positive(), restoreRevision: z.number().int().positive() }).strict().parse(request.body)
const actor = actorOf(request.authUser), principal = await ownerPrincipal(connectionId, actor, ixRestoreOperation)
await auditOwnerRequest(actor.id, "ix.web.restore", connectionId, { segmentId: input.id, revision: input.restoreRevision })
return ownerDelegate(() => invokeMcpTool(principal, "mm_ix_restore_plan", input))
})
app.post("/mcp/secrets", async request => {
const input = z.object({ purpose: z.string().min(1).max(512), serverId: z.number().int().positive().optional(), value: z.string().min(1).max(1000000) }).strict().parse(request.body), id = randomUUID(), actor = actorOf(request.authUser)
await connectionWrite(async client => { await client.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [id, input.purpose, input.serverId ?? null, await encryptSecret(input.value), actor.id]); await audit(client, actor.id, "secret.create", null, { secretRef: id, purpose: input.purpose, serverId: input.serverId }) })
@@ -0,0 +1,30 @@
import { isIP } from "node:net"
import type { IxSite } from "@mmapp/contracts/internet-exchange"
import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js"
import { IxConflict } from "./internet-exchange-plan.js"
type Binding = IxSite["interfaces"][number]
export function ixWireguardClientTemplate(binding: Binding, hubPublicKey: string, routes: string[]) {
const config = binding.provisionWireguard
if (binding.kind !== "wireguard" || !config?.hubEndpoint || !/^[A-Za-z0-9+/]{43}=$/.test(hubPublicKey)) {
throw new IxConflict("Для Linux нужны публичный ключ узла и заданный внешний адрес IX")
}
const serverAddress = config.localAddress.split("/")[0]
const family = isIP(serverAddress)
const address = (value: string) => value + (family === 4 ? "/32" : "/128")
const endpoint = isIP(config.hubEndpoint) === 6 ? "[" + config.hubEndpoint + "]" : config.hubEndpoint
return [
"[Interface]",
"Address = " + address(config.peerAddress),
"PrivateKey = <локальный_приватный_ключ>",
"Table = off",
"",
"[Peer]",
"PublicKey = " + hubPublicKey,
"Endpoint = " + endpoint + ":" + config.listenPort,
"AllowedIPs = " + [...new Set([address(serverAddress), ...routes.map(canonicalBgpPolicyPrefix)])].join(", "),
"PersistentKeepalive = 25",
"",
].join("\n")
}
@@ -0,0 +1,57 @@
import assert from "node:assert/strict"
import { test } from "node:test"
import type { IxRouterObject } from "@mmapp/contracts/internet-exchange"
import { planIxNative } from "./internet-exchange-execution.js"
import { ixNamespace } from "./internet-exchange-plan.js"
const id = "aaaaaaaa-1111-4222-8333-123456789abc"
const name = ixNamespace(id)
const vrf: IxRouterObject = {
serverId: 17, path: "/ip/vrf", fields: { name, comment: name + ":vrf", interfaces: "wg-ix" },
}
const route: IxRouterObject = {
serverId: 17, path: "/ip/route", fields: { comment: name + ":exit-4-0", "routing-table": name, "dst-address": "0.0.0.0/0", gateway: "192.0.2.1@" + name },
}
const device = (rows: Record<string, Array<Record<string, string>>>) => [{ serverId: 17, source: "", rows }]
test("a new VRF is inserted before main and no foreign route is altered", () => {
const changes = planIxNative(id, [vrf, route], [], device({
"/ip/vrf": [{ ".id": "*A", name: "main", interfaces: "all", builtin: "true" }],
"/ip/route": [{ ".id": "*B", "routing-table": "main", "dst-address": "0.0.0.0/0", gateway: "203.0.113.1" }],
}))
assert.deepEqual(changes.map(task => ({ path: task.path, position: task.placeBefore, id: task.rosId })),
[{ path: "/ip/vrf", position: "*A", id: undefined }, { path: "/ip/route", position: undefined, id: undefined }])
})
test("managed changes update one exact ID and never adopt unknown objects after an interrupted job", () => {
const state = device({ "/ip/route": [{ ".id": "*B", ...route.fields }] })
const next = { ...route, fields: { ...route.fields, gateway: "192.0.2.2@" + name } }
const [task] = planIxNative(id, [next], [route], state)
assert.equal(task.rosId, "*B")
assert.deepEqual(task.fields, { gateway: "192.0.2.2@" + name })
assert.throws(() => planIxNative(id, [next], [], state), /незавершённой операции/)
assert.throws(() => planIxNative(id, [next], [route], device({ "/ip/route": [{ ".id": "*B", ...next.fields }] })), /изменён вне операции/)
})
test("a foreign route in the IX table and implicit native field removal are refused", () => {
assert.throws(() => planIxNative(id, [route], [], device({
"/ip/route": [{ ".id": "*B", "routing-table": name, "dst-address": "192.0.2.0/24" }],
})), /пересекается с существующей/)
const { gateway: _gateway, ...fields } = route.fields
assert.throws(() => planIxNative(id, [{ ...route, fields }], [route], device({ "/ip/route": [{ ".id": "*B", ...route.fields }] })), /неявно сбросить/)
})
test("IX does not take a main-table service address from another route", () => {
const resolver = { ...route, fields: { ...route.fields, comment: name + ":bgp-resolve-test", "routing-table": "main", "dst-address": "198.18.0.101/32" } }
const occupied = { ".id": "*B", "routing-table": "main", "dst-address": "198.18.0.101/32", dynamic: "true" }
assert.throws(() => planIxNative(id, [resolver], [], device({ "/ip/route": [occupied] })), /уже занят/)
})
test("RouterOS REST host matches allow only the equivalent explicit host length", () => {
for (const [address, hostLength, wider] of [["192.0.2.1", "32", "24"], ["2001:db8::1", "128", "64"]]) {
const rule = { ...route, path: "/ip/firewall/filter", fields: { comment: name + ":host", "src-address": address + "/" + hostLength } }
const rows = (source: string) => device({ [rule.path]: [{ ".id": "*B", ...rule.fields, "src-address": source }] })
assert.deepEqual(planIxNative(id, [rule], [rule], rows(address)), [])
assert.throws(() => planIxNative(id, [rule], [rule], rows(address + "/" + wider)), /изменён вне операции/)
}
})
@@ -0,0 +1,372 @@
import { randomUUID } from "node:crypto"
import { isIP } from "node:net"
import { setTimeout as delay } from "node:timers/promises"
import { ixChangeSchema, type IxChange, type IxConfiguration, type IxRouterObject, type IxSegment, type IxSiteObservation } from "@mmapp/contracts/internet-exchange"
import { fingerprint } from "../modules/mcp/security.js"
import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js"
import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js"
import { assertVerifiedConfigurationAccess } from "./config-mutation-policy.js"
import { compileIxConfiguration, IxConflict, ixDeviceIds, ixDiscardInterface, ixNamespace } from "./internet-exchange-plan.js"
import { assertIxName, assertIxRevision, assertIxSites, readIxRevision, readIxSegment, writeIxRevision } from "./internet-exchange-store.js"
import { MikrotikClient } from "./mikrotik.js"
import { ixWireguardClientTemplate } from "./internet-exchange-client.js"
type NativeRow = Record<string, string>
type IpCore = Pick<MikrotikClient, "get" | "put" | "patch" | "delete">
interface NativeDevice {
serverId: number
source: string
rows: Record<string, NativeRow[]>
}
interface Task { serverId: number; path: string; fields?: Record<string, string>; before?: NativeRow; rosId?: string; placeBefore?: string }
interface IxNativeChange {
objects: IxRouterObject[]
before: NativeDevice[]
tasks: Task[]
}
const routerId = /^\*[a-z\d]+$/i
const marker = (path: string, comment: string) => path + "\0" + comment
const objectMarker = (object: IxRouterObject) => marker(object.path, object.fields.comment)
const deviceMarker = (object: IxRouterObject) => object.serverId + "\0" + objectMarker(object)
const isTrue = (value: unknown) => value === true || value === "true" || value === "yes"
const columns = [".id", "comment", "name", "type", "disabled", "dynamic", "builtin", "interfaces", "routing-table", "dst-address", "gateway", "chain"]
const filterPath = (path: string) => /^\/(ip|ipv6)\/firewall\/(filter|nat)$/.test(path)
function normalize(field: string, value: string | undefined) {
if (["disabled", "advertise", "blackhole", "multihop"].includes(field)) return value === undefined ? null : isTrue(value) ? "yes" : "no"
if (field === "rule") return value?.trim()
if (field === "interfaces") return value?.split(",").sort().join(",")
if (value && ["src-address", "dst-address", "remote.address"].includes(field)) {
const slash = value.lastIndexOf("/")
const address = value.slice(0, slash)
if ((value.endsWith("/32") && isIP(address) === 4) || (value.endsWith("/128") && isIP(address) === 6)) return address
}
return value
}
function matches(row: NativeRow, fields: Record<string, string>) {
return Object.entries(fields).every(([field, value]) => normalize(field, row[field]) === normalize(field, value))
}
function rowsFor(device: NativeDevice, path: string) { return device.rows[path] ?? [] }
function managedRow(device: NativeDevice, object: IxRouterObject) {
const matches = rowsFor(device, object.path).filter(row => row.comment === object.fields.comment)
if (matches.length > 1) throw new IxConflict("Дублируется идентификатор объекта IX на RouterOS")
return matches[0]
}
function verifyNativeId(row: NativeRow) {
if (!routerId.test(row[".id"] ?? "") || isTrue(row.dynamic) || isTrue(row.builtin)) throw new IxConflict("Объект IX не имеет подтверждённого статического ID")
return row[".id"]
}
export function planIxNative(id: string, desired: IxRouterObject[], previous: IxRouterObject[], devices: NativeDevice[]): Task[] {
const namespace = ixNamespace(id)
const desiredKeys = new Set(desired.map(deviceMarker))
const previousKeys = new Set(previous.map(deviceMarker))
const result: Task[] = []
const interfaceCleanup: Task[] = []
// Never remove user objects by name or rebuild a whole RouterOS resource table.
for (const object of [...previous].reverse()) {
if (desiredKeys.has(deviceMarker(object))) continue
const device = devices.find(entry => entry.serverId === object.serverId)!
const row = managedRow(device, object)
if (!row || !matches(row, object.fields)) throw new IxConflict("Объект предыдущей ревизии IX изменён вне операции")
const steps = object.path === "/interface/wireguard" ? interfaceCleanup : result
steps.push({ serverId: object.serverId, path: object.path, rosId: verifyNativeId(row), before: row })
}
for (const object of desired) {
const device = devices.find(entry => entry.serverId === object.serverId)!
const path = object.path, row = managedRow(device, object)
if (object.fields["routing-table"] === "main" &&
rowsFor(device, path).some(entry => entry !== row && entry["routing-table"] === "main" && entry["dst-address"] === object.fields["dst-address"])) {
throw new IxConflict("Служебный next hop BGP уже занят в таблице main")
}
if (row && !previousKeys.has(deviceMarker(object))) throw new IxConflict("Найден объект незавершённой операции IX. Сначала выполните сверку")
if (row) {
const before = previous.find(entry => deviceMarker(entry) === deviceMarker(object))!
if (!matches(row, before.fields)) throw new IxConflict("Объект IX изменён вне операции")
if (Object.keys(before.fields).some(field => !(field in object.fields))) {
throw new IxConflict("Поле RouterOS нельзя неявно сбросить. Замените объект с новым идентификатором")
}
const fields = Object.fromEntries(Object.entries(object.fields).filter(([field, value]) => normalize(field, row[field]) !== normalize(field, value)))
if (Object.keys(fields).length) result.push({ serverId: object.serverId, path, rosId: verifyNativeId(row), fields, before: row })
continue
}
if (previousKeys.has(deviceMarker(object))) throw new IxConflict("Объект предыдущей ревизии IX исчез. Сначала выполните сверку")
const foreign = rowsFor(device, path).some(entry => !entry.comment?.startsWith(namespace + ":") &&
(object.fields.name && entry.name === object.fields.name || object.fields.chain && entry.chain === object.fields.chain && path === "/routing/filter/rule" ||
object.fields["routing-table"] && entry["routing-table"] === namespace && !isTrue(entry.dynamic)))
if (foreign) throw new IxConflict("Объект IX пересекается с существующей конфигурацией RouterOS")
const placeBefore = path === "/ip/vrf" ? rowsFor(device, path).find(entry => entry.name === "main")?.[".id"] :
filterPath(path) ? rowsFor(device, path)[0]?.[".id"] : undefined
if (path === "/ip/vrf" && !routerId.test(placeBefore ?? "")) throw new IxConflict("Не подтверждена позиция VRF main")
if (placeBefore && !routerId.test(placeBefore)) throw new IxConflict("Не подтверждена позиция правила фильтра")
result.push({ serverId: object.serverId, path, fields: object.fields, placeBefore })
}
return result.concat(interfaceCleanup)
}
async function readRows(client: IpCore, path: string, props: readonly string[]) {
const raw = await client.get<unknown>(path + "?" + new URLSearchParams({ ".proplist": [...new Set(props)].join(",") }))
if (!Array.isArray(raw) || raw.length > 10000 || raw.some(value => !value || typeof value !== "object" || Array.isArray(value))) {
throw new IxConflict("Не удалось прочитать полный каталог RouterOS")
}
return raw.map(entry => Object.fromEntries(props.flatMap(key => {
const value = entry[key]
return typeof value === "string" || typeof value === "boolean" || typeof value === "number" ? [[key, String(value)]] : []
}))) as NativeRow[]
}
async function readDevice(server: ServerRow, objects: IxRouterObject[]): Promise<NativeDevice> {
const client = MikrotikClient.fromServer(server)
const fieldsByPath = new Map<string, string[]>([["/interface", columns], ["/interface/bridge/port", [".id", "bridge", "interface"]],
["/ip/vrf", columns], ["/ip/route", columns], ["/ipv6/route", columns]])
for (const object of objects) {
fieldsByPath.set(object.path, [...new Set([...(fieldsByPath.get(object.path) ?? columns),
...(filterPath(object.path) ? ["invalid"] : []), ...Object.keys(object.fields)])])
}
if (objects.some(object => object.fields["ipsec-policy"])) {
fieldsByPath.set("/interface/gre", [".id", "name", "local-address", "remote-address", "allow-fast-path"])
fieldsByPath.set("/ip/ipsec/policy", [".id", "action", "level", "tunnel", "protocol", "src-address", "dst-address", "disabled", "template"])
}
const rows: Record<string, NativeRow[]> = {}
for (const [path, keys] of fieldsByPath) rows[path] = await readRows(client, path, keys)
for (const path of ["/ip/route", "/ipv6/route"]) {
const reserved = new Set(objects.filter(object => object.path === path && object.fields["routing-table"] === "main").map(object => object.fields["dst-address"]))
rows[path] = rows[path].filter(row => !isTrue(row.dynamic) || (row["routing-table"] === "main" && reserved.has(row["dst-address"])))
}
return { serverId: server.id, source: fingerprint(server), rows }
}
function validateBindings(configuration: IxConfiguration, id: string, devices: NativeDevice[]) {
for (const site of configuration.sites) {
const device = devices.find(entry => entry.serverId === site.serverId)!
if (rowsFor(device, "/interface/bridge/port").some(row => row.bridge === ixDiscardInterface(id))) {
throw new IxConflict("Изолированный bridge отбрасывания IX содержит порты")
}
for (const binding of site.interfaces) {
const iface = rowsFor(device, "/interface").find(row => row.name === binding.name)
if (!iface && binding.provisionWireguard) continue
const expected = binding.kind === "wireguard" ? "wg" : binding.kind === "gre-ipsec" ? "gre-tunnel" : null
if (!iface || (expected && iface.type !== expected && !(expected === "wg" && iface.type === "wireguard") &&
!(expected === "gre-tunnel" && iface.type === "gre"))) {
throw new IxConflict("Не найден подготовленный интерфейс " + binding.name)
}
// Moving an existing interface into a VRF affects the entire interface.
// Explicit staging on the CHR separates IX handoff from existing services.
if (iface.comment !== "IX:" + configuration.name && !(binding.provisionWireguard && iface.comment?.startsWith(ixNamespace(id) + ":wg-"))) {
throw new IxConflict("Интерфейс " + binding.name + " не выделен для данного сегмента IX")
}
const foreignVrf = rowsFor(device, "/ip/vrf").find(row => row.name !== "main" && row.name !== ixNamespace(id) && row.interfaces?.split(",").includes(binding.name))
const mainRoutes = [...rowsFor(device, "/ip/route"), ...rowsFor(device, "/ipv6/route")].filter(row => row["routing-table"] === "main" && !isTrue(row.dynamic))
if (foreignVrf || mainRoutes.some(row => row.gateway === binding.name || row.gateway?.includes("%" + binding.name))) {
throw new IxConflict("Интерфейс " + binding.name + " уже используется другой маршрутизацией")
}
if (binding.kind === "gre-ipsec") {
const gre = rowsFor(device, "/interface/gre").find(row => row.name === binding.name)
const policy = rowsFor(device, "/ip/ipsec/policy").some(row =>
!isTrue(row.disabled) && !isTrue(row.template) && !isTrue(row.tunnel) &&
row.action === "encrypt" && ["require", "unique"].includes(row.level) && ["gre", "47"].includes(row.protocol) &&
row["src-address"] === binding.localEndpoint + "/32" && row["dst-address"] === binding.endpoint + "/32")
if (!gre || gre["local-address"] !== binding.localEndpoint || gre["remote-address"] !== binding.endpoint || !policy) {
throw new IxConflict("Для " + binding.name + " не подтверждены адреса и обязательная политика GRE/IPsec")
}
if (!["false", "no"].includes(gre["allow-fast-path"])) {
throw new IxConflict("Отключите FastPath на интерфейсе GRE/IPsec " + binding.name)
}
}
}
}
}
async function prepareNative(id: string, configuration: IxConfiguration, previousConfiguration: IxConfiguration | null): Promise<IxNativeChange> {
const objects = compileIxConfiguration(id, configuration)
const previous = previousConfiguration ? compileIxConfiguration(id, previousConfiguration) : []
const before: NativeDevice[] = []
for (const serverId of [...new Set([...objects, ...previous].map(object => object.serverId))].sort((a, b) => a - b)) {
const server = await getServerRowById(serverId)
if (!server?.enabled || server.type !== "ix-node") throw new IxConflict("Площадка IX отключена или изменила тип")
before.push(await readDevice(server, [...objects, ...previous].filter(object => object.serverId === serverId)))
}
validateBindings(configuration, id, before)
for (const old of previousConfiguration?.sites ?? []) {
const next = configuration.sites.find(site => site.serverId === old.serverId)
const device = before.find(device => device.serverId === old.serverId)!
for (const binding of old.interfaces.filter(binding => !next?.interfaces.some(candidate => candidate.name === binding.name))) {
if (!rowsFor(device, "/interface").some(row => row.name === binding.name && isTrue(row.disabled))) {
throw new IxConflict("Отключите интерфейс перед исключением из VRF IX: " + binding.name)
}
}
}
return { objects, before, tasks: planIxNative(id, objects, previous, before) }
}
export async function ixTarget(change: IxChange) {
const current = change.id ? await readIxSegment(change.id) : null
const applied = current?.appliedRevision && current.id ? await readIxRevision(current.id, current.appliedRevision) : null
return [...new Set([...ixDeviceIds(change.configuration), ...(current ? ixDeviceIds(current.configuration) : []), ...(applied ? ixDeviceIds(applied) : [])])].sort((a, b) => a - b)
}
export async function prepareIxChange(raw: unknown) {
const change = ixChangeSchema.parse(raw)
const id = change.id ?? randomUUID()
const current = change.id ? await readIxSegment(id) : null
assertIxRevision(current, change.revision)
await assertIxName(id, change.configuration.name)
await assertIxSites(change.configuration)
const previous = current?.appliedRevision ? await readIxRevision(id, current.appliedRevision) : null
const native = await prepareNative(id, change.configuration, previous)
const before = { revision: current?.revision ?? 0, devices: fingerprint(native.before) }
const objects = native.before.flatMap(device => [{ kind: "server", id: String(device.serverId), serverId: device.serverId }])
return { id, change, before, native, objects }
}
export async function currentIxChange(id: string, change: IxChange) {
const current = await readIxSegment(id)
if (current?.revision !== change.revision && !(current === null && change.revision === 0)) return { revision: current?.revision ?? 0 }
const applied = current?.appliedRevision ? await readIxRevision(id, current.appliedRevision) : null
const native = await prepareNative(id, change.configuration, applied)
return { revision: current?.revision ?? 0, devices: fingerprint(native.before) }
}
export async function reconcileIxChange(id: string, change: IxChange, expected: { revision: number; devices: string }, authorize: () => Promise<void>) {
const current = await readIxSegment(id)
if (current?.revision === expected.revision + 1 && current.appliedRevision === current.revision &&
fingerprint(current.configuration) === fingerprint(change.configuration)) {
const desired = compileIxConfiguration(id, change.configuration)
let matchesDesiredState = true
for (const site of current.configuration.sites) {
await authorize()
const server = await getServerRowById(site.serverId)
if (!server?.enabled) return { matchesDesiredState: false, matchesOriginalState: false }
const objects = desired.filter(object => object.serverId === site.serverId)
const observed = await readDevice(server, objects)
matchesDesiredState &&= objects.every(object => {
const row = managedRow(observed, object)
return row && !isTrue(row.invalid) && matches(row, object.fields)
})
}
return { matchesDesiredState, matchesOriginalState: false }
}
try {
await authorize()
return { matchesDesiredState: false, matchesOriginalState: fingerprint(await currentIxChange(id, change)) === fingerprint(expected) }
} catch {
return { matchesDesiredState: false, matchesOriginalState: false }
}
}
export async function observeIxSegment(segment: IxSegment): Promise<IxSiteObservation[]> {
if (!segment.appliedRevision) return []
const applied = await readIxRevision(segment.id, segment.appliedRevision)
const namespace = ixNamespace(segment.id)
return Promise.all(applied.sites.map(async ({ serverId }) => {
const observedAt = new Date().toISOString()
try {
const server = await getServerRowById(serverId)
if (!server?.enabled || server.type !== "ix-node") throw new IxConflict("Площадка IX недоступна")
const client = MikrotikClient.fromServer(server)
const routeFields = ["dst-address", "routing-table", "gateway", "immediate-gw", "active"]
const lookup = new URLSearchParams({ ".proplist": routeFields.join(","), "routing-table": namespace })
const [routes, sessions] = await Promise.all([
client.get<unknown>("/routing/route?" + lookup),
readRows(client, "/routing/bgp/session", ["name", "established"]),
])
if (!Array.isArray(routes) || routes.length > 10000) throw new IxConflict("Таблица IX недоступна")
return {
serverId, observedAt, available: true,
routes: routes.filter(row => row && row["routing-table"] === namespace && typeof row["dst-address"] === "string").slice(0, 100).map(row => ({
prefix: row["dst-address"], gateway: typeof row.gateway === "string" ? row.gateway : "",
immediateGateway: typeof row["immediate-gw"] === "string" ? row["immediate-gw"] : null,
active: isTrue(row.active),
})),
neighbors: sessions.filter(row => row.name?.startsWith(namespace + "-")).map(row => ({ name: row.name, established: isTrue(row.established) })),
}
} catch {
return { serverId, observedAt, available: false, routes: [], neighbors: [] }
}
}))
}
export async function readIxLinuxClient(segment: IxSegment, serverId: number, name: string) {
if (segment.appliedRevision === null) throw new IxConflict("Сначала примените конфигурацию площадки")
const configuration = await readIxRevision(segment.id, segment.appliedRevision)
const site = configuration.sites.find(site => site.serverId === serverId)
const binding = site?.interfaces.find(entry => entry.name === name)
const server = await getServerRowById(serverId)
if (!site || !binding || !server?.enabled || server.type !== "ix-node") throw new IxConflict("Подключение IX не найдено", 404)
const wireguard = await readRows(MikrotikClient.fromServer(server), "/interface/wireguard", ["name", "public-key", "comment"])
const iface = wireguard.find(row => row.name === binding.name && row.comment?.startsWith(ixNamespace(segment.id) + ":wg-"))
const routes = configuration.sites.flatMap(entry => entry.interfaces
.filter(connection => connection !== binding && connection.kind !== "uplink").flatMap(connection => connection.prefixes))
const peer = site.neighbors.find(entry => entry.binding === name)
return {
template: ixWireguardClientTemplate(binding, iface?.["public-key"] ?? "", routes),
...(peer ? { bgp: { localAs: peer.remoteAs, remoteAs: site.autonomousSystem, neighbor: peer.localAddress, advertisedPrefixes: peer.prefixes } } : {}),
}
}
export async function applyIxChange(id: string, change: IxChange, expected: { revision: number; devices: string }, actorId: string) {
return withApplicationMutationLock(async () => withConfigMutationLock(await ixTarget(change), async () => {
await assertVerifiedConfigurationAccess()
const current = await readIxSegment(id)
assertIxRevision(current, expected.revision)
await assertIxName(id, change.configuration.name)
const applied = current?.appliedRevision ? await readIxRevision(id, current.appliedRevision) : null
const native = await prepareNative(id, change.configuration, applied)
if (fingerprint(native.before) !== expected.devices) throw new IxConflict("Конфигурация IX изменилась после предпросмотра")
for (const task of native.tasks) {
await assertVerifiedConfigurationAccess()
const server = await getServerRowById(task.serverId)
if (!server?.enabled || fingerprint(server) !== native.before.find(device => device.serverId === task.serverId)?.source) {
throw new IxConflict("Изменился источник конфигурации IX")
}
const client = MikrotikClient.fromServer(server)
const nativePath = task.rosId ? task.path + "/" + encodeURIComponent(task.rosId) : task.path
if (task.before) {
const observed = (await readRows(client, task.path, Object.keys(task.before))).find(row => row[".id"] === task.rosId)
if (!observed || !matches(observed, task.before)) throw new IxConflict("Объект IX изменился перед записью")
} else if (task.fields && (await readRows(client, task.path, [".id", "comment", "name"])).some(row =>
row.comment === task.fields?.comment || task.fields?.name && row.name === task.fields.name)) {
throw new IxConflict("Объект IX был создан после подготовки плана")
}
if (!task.fields) {
await client.delete(nativePath)
if ((await readRows(client, task.path, [".id"])).some(row => row[".id"] === task.rosId)) throw new IxConflict("Удаление объекта IX не подтверждено")
continue
}
const result = task.rosId
? await client.patch<NativeRow>(nativePath, task.fields)
: await client.put<NativeRow>(nativePath, { ...task.fields, ...(task.placeBefore ? { "place-before": task.placeBefore } : {}) })
const returnedId = task.rosId ?? result?.[".id"]
if (!returnedId || !routerId.test(returnedId)) throw new IxConflict("RouterOS не подтвердил ID объекта IX")
const row = (await readRows(client, task.path, [".id", ...Object.keys(task.fields)])).find(entry => entry[".id"] === returnedId)
if (!row || !matches(row, task.fields)) throw new IxConflict("Перечитанный объект IX не совпал с планом")
const vrfName = task.path === "/ip/vrf" ? task.fields.name : undefined
if (vrfName) {
const deadline = Date.now() + 3000
while (!(await readRows(client, "/interface", ["name"])).some(entry => entry.name === vrfName)) {
if (Date.now() >= deadline) throw new IxConflict("RouterOS не создал интерфейс VRF IX")
await delay(100)
}
}
}
await assertVerifiedConfigurationAccess()
for (const device of native.before) {
const server = await getServerRowById(device.serverId)
if (!server || fingerprint(server) !== device.source) throw new IxConflict("Источник IX изменился во время проверки")
const desired = native.objects.filter(object => object.serverId === device.serverId)
const after = await readDevice(server, desired)
if (rowsFor(after, "/interface/bridge/port").some(row => row.bridge === ixDiscardInterface(id))) throw new IxConflict("Нарушена изоляция bridge отбрасывания IX")
if (!desired.every(object => {
const row = managedRow(after, object)
return row && !isTrue(row.invalid) && matches(row, object.fields)
})) throw new IxConflict("Итоговая конфигурация IX не совпала с планом")
}
await assertVerifiedConfigurationAccess()
const segment = await writeIxRevision(id, expected.revision, change.configuration, actorId, true)
return { segmentId: segment.id, revision: segment.revision, configurationVerified: true, forwardingVerified: false, changedObjects: native.tasks.length }
}), { waitMs: 30_000 })
}
@@ -0,0 +1,90 @@
import assert from "node:assert/strict"
import { test } from "node:test"
import { ixSegmentConfigurationSchema, type IxConfiguration } from "@mmapp/contracts/internet-exchange"
import { compileIxConfiguration, ixDiscardInterface, ixNamespace } from "./internet-exchange-plan.js"
const id = "aaaaaaaa-1111-4222-8333-123456789abc"
const table = ixNamespace(id)
const fixture = (): IxConfiguration => ixSegmentConfigurationSchema.parse({
name: "compute",
sites: [{
serverId: 17, autonomousSystem: 64512, routerId: "192.0.2.17",
interfaces: [{ name: "wg-eu", kind: "wireguard" }, { name: "wg-arn", kind: "wireguard", peerServerId: 1 },
{ name: "uplink", kind: "uplink", natIpv4: true }, { name: "wg-feed", kind: "wireguard" }],
groups: [
{ id: "eu", gateway: "198.18.0.1", primary: { interface: "wg-eu", nextHop: "10.0.1.2", probe: "192.0.2.101" },
backup: { interface: "wg-arn", nextHop: "10.0.2.2", probe: "192.0.2.102" } },
{ id: "exit", gateway: "198.18.0.2", primary: { interface: "uplink", nextHop: "10.0.3.2", probe: "192.0.2.103" } },
],
exits: ["exit"],
neighbors: [{ id: "evo", type: "public-feed", binding: "wg-feed", address: "10.0.4.2", localAddress: "10.0.4.1", remoteAs: 64513,
rules: [{ community: "65000:120", group: "eu" }] }],
}],
})
test("a BGP community uses a stable recursive next hop with independent probes and a final discard", () => {
const objects = compileIxConfiguration(id, fixture())
const policy = objects.find(object => object.fields.comment.endsWith(":bgp-in-evo"))!.fields.rule
assert.match(policy, /if \(dst == 0\.0\.0\.0\/0\) \{ reject; \}/)
assert.match(policy, /bgp-communities includes 65000:120\) \{ set gw 198\.18\.0\.1; set scope-target 23; accept;/)
assert.match(policy, /reject;$/)
const group = objects.filter(object => object.fields["dst-address"] === "198.18.0.1/32" && object.fields["routing-table"] === table)
assert.deepEqual(group.map(object => object.fields.distance), ["10", "11", "254"])
assert.deepEqual(group.slice(0, 2).map(object => object.fields.gateway), ["192.0.2.101@" + table, "192.0.2.102@" + table])
assert.ok(group[2].fields.gateway.endsWith("%" + ixDiscardInterface(id) + "@" + table))
assert.ok(!ixDiscardInterface(id).startsWith(table))
assert.ok(objects.some(object => object.fields["src-port"] === "179" && object.fields["connection-state"] === "established" &&
object.fields["src-address"] === "10.0.4.2/32" && object.fields["in-interface"] === table))
assert.deepEqual(objects.filter(object => object.fields["routing-table"] === "main").map(object => object.fields), [{
"routing-table": "main", "dst-address": "198.18.0.1/32", gateway: "198.18.0.1@" + table, scope: "22", "target-scope": "21", comment: table + ":bgp-resolve-eu",
}])
assert.ok(objects.some(object => object.path === "/ip/firewall/nat" && object.fields["out-interface"] === "uplink"))
})
test("ordinary exit is used for the prefix only when its group explicitly enables fallback", () => {
const value = fixture()
value.sites[0].groups[0].onFailure = "segment-exit"
const group = compileIxConfiguration(id, value).filter(object => object.fields["dst-address"] === "198.18.0.1/32" && object.fields["routing-table"] === table)
assert.equal(group.length, 4)
assert.equal(group[2].fields.gateway, "192.0.2.103@" + table)
assert.equal(group[3].fields.distance, "254")
})
test("a BGP resolver cannot claim an ordinary address in main", () => {
const value = fixture()
value.sites[0].groups[0].gateway = "8.8.8.8"
assert.throws(() => compileIxConfiguration(id, value), /служебный next hop BGP/)
})
test("a probe cannot silently select another interface or recursively follow its own group", () => {
const value = fixture()
value.sites[0].groups[0].backup!.probe = value.sites[0].groups[0].primary.probe
assert.throws(() => compileIxConfiguration(id, value), /закреплён за несколькими путями/)
value.sites[0].groups[0].backup!.probe = value.sites[0].groups[0].gateway
assert.throws(() => compileIxConfiguration(id, value), /не может быть целью пробы/)
})
test("private announcements are restricted to their binding and retain a discard route on withdrawal", () => {
const value = fixture(), site = value.sites[0]
site.interfaces[0].prefixes = ["10.70.0.0/16"]
const peer = site.neighbors[0]
peer.type = "member"
peer.rules = []
peer.binding = "wg-eu"
peer.group = "eu"
peer.prefixes = ["10.70.0.0/16"]
assert.ok(compileIxConfiguration(id, value).some(object => object.fields["dst-address"] === "10.70.0.0/16" && object.fields.blackhole === "yes"))
peer.prefixes = ["10.71.0.0/16"]
assert.throws(() => compileIxConfiguration(id, value), /только диапазоны подключения/)
})
test("GRE/IPsec binds its plaintext drops to the two fixed outer endpoints", () => {
const value = fixture()
const binding = value.sites[0].interfaces[0]
binding.kind = "gre-ipsec"
assert.throws(() => compileIxConfiguration(id, value), /фиксированных внешних/)
binding.localEndpoint = "192.0.2.44"
binding.endpoint = "192.0.2.45"
const drop = compileIxConfiguration(id, value).find(object => object.fields["ipsec-policy"] === "in,none")!
assert.deepEqual([drop.fields["src-address"], drop.fields["dst-address"]], ["192.0.2.45/32", "192.0.2.44/32"])
})
@@ -0,0 +1,274 @@
import { BlockList, isIP } from "node:net"
import type { IxConfiguration, IxRouterObject, IxSite } from "@mmapp/contracts/internet-exchange"
import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js"
export class IxConflict extends Error {
constructor(message: string, readonly statusCode = 409) { super(message) }
}
const families = [4, 6] as const
type Family = typeof families[number]
const familyOf = (address: string) => isIP(address.split("/")[0])
const host = (address: string) => canonicalBgpPolicyPrefix(address + (isIP(address) === 4 ? "/32" : "/128"))
const routePath = (family: number) => family === 4 ? "/ip/route" : "/ipv6/route"
const familyName = (family: number) => family === 4 ? "ip" : "ipv6"
const sink = {
4: { local: "169.254.255.249/30", neighbor: "169.254.255.250" },
6: { local: "fd00:ffff:ffff::1/126", neighbor: "fd00:ffff:ffff::2" },
}
const defaultRoute = { 4: "0.0.0.0/0", 6: "::/0" }
const bgpGatewayRange = { 4: "198.18.0.0/15", 6: "fd00:198:18::/48" }
const forbiddenPublic = {
4: ["0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.168.0.0/16", "198.18.0.0/15", "224.0.0.0/3"],
6: ["::/96", "::ffff:0:0/96", "fc00::/7", "fe80::/10", "ff00::/8"],
}
export function ixNamespace(id: string) { return "ix-" + id.replace(/-/g, "").slice(0, 18) }
// RouterOS accepts unambiguous prefixes for interface names before the VRF interface appears.
export function ixDiscardInterface(id: string) { return "ix-drop-" + ixNamespace(id).slice(3) }
export function ixDeviceIds(configuration: IxConfiguration) {
return [...new Set(configuration.sites.flatMap(site => [site.serverId, ...site.interfaces.flatMap(binding => binding.peerServerId === undefined ? [] : [binding.peerServerId])]))].sort((a, b) => a - b)
}
function requireIx(condition: unknown, message: string): asserts condition {
if (!condition) throw new IxConflict(message, 400)
}
function unique(values: string[], label: string) {
requireIx(new Set(values).size === values.length, "Повторяется " + label)
}
function inNetwork(cidr: string, value: string) {
const [network, length] = cidr.split("/")
const [candidate, candidateLength] = value.split("/")
const family = isIP(network)
if (family !== isIP(candidate) || (candidateLength !== undefined && Number(candidateLength) < Number(length))) return false
const familyName = family === 4 ? "ipv4" : "ipv6"
const block = new BlockList()
block.addSubnet(network, Number(length), familyName)
return block.check(candidate, familyName)
}
function groupPaths(group: IxSite["groups"][number]) {
return group.backup ? [group.primary, group.backup] : [group.primary]
}
function validateSite(site: IxSite, otherId?: number) {
unique(site.interfaces.map(entry => entry.name), "интерфейс IX")
unique(site.groups.map(entry => entry.id), "группа шлюзов")
unique(site.groups.map(entry => host(entry.gateway)), "служебный next hop")
unique(site.neighbors.map(entry => entry.id), "BGP-сосед")
unique(site.neighbors.map(entry => host(entry.address)), "адрес BGP-соседа")
unique(site.routes.map(entry => canonicalBgpPolicyPrefix(entry.prefix)), "префикс")
unique(site.exits, "интернет-выход")
const bindings = new Map(site.interfaces.map(binding => [binding.name, binding]))
const groups = new Map(site.groups.map(group => [group.id, group]))
for (const binding of site.interfaces) {
if (binding.kind === "gre-ipsec") {
requireIx(binding.endpoint && binding.localEndpoint && isIP(binding.endpoint) === 4 && isIP(binding.localEndpoint) === 4 &&
binding.endpoint !== binding.localEndpoint, "GRE/IPsec требует двух разных фиксированных внешних IPv4-адресов")
}
if (binding.natIpv4) requireIx(binding.kind === "uplink" && !binding.peerServerId, "NAT разрешён только на локальном uplink")
const wireguard = binding.provisionWireguard
if (wireguard) {
requireIx(binding.kind === "wireguard" && inNetwork(wireguard.localAddress, wireguard.peerAddress) &&
host(wireguard.localAddress.split("/")[0]) !== host(wireguard.peerAddress), "Неверные адреса WireGuard-подключения")
requireIx(!wireguard.endpointPort || binding.endpoint, "Порт WireGuard требует внешнего адреса")
requireIx(!binding.endpoint || wireguard.endpointPort, "Внешний адрес WireGuard требует порта")
}
}
const probes = new Map<string, string>()
const ownedNetworks = site.interfaces.flatMap(binding => binding.prefixes.map(prefix => ({ binding, prefix })))
for (const [index, first] of ownedNetworks.entries()) {
requireIx(Number(first.prefix.split("/")[1]) > 0, "Префикс подключения не может быть default route")
for (const second of ownedNetworks.slice(0, index)) {
if (first.binding === second.binding) continue
requireIx(!inNetwork(first.prefix, second.prefix) && !inNetwork(second.prefix, first.prefix), "Префиксы разных подключений пересекаются")
}
}
for (const group of site.groups) {
const family = familyOf(group.gateway) as Family
requireIx(!ownedNetworks.some(entry => inNetwork(entry.prefix, group.gateway)), "Служебный next hop пересекает сеть подключения")
requireIx(!inNetwork(sink[family].local, group.gateway), "Служебный next hop пересекает сеть отбрасывания")
if (group.backup) requireIx(group.primary.interface !== group.backup.interface, "Резерв требует другого интерфейса")
for (const candidate of groupPaths(group)) {
requireIx(bindings.has(candidate.interface), "Шлюз использует необъявленный интерфейс")
requireIx([candidate.nextHop, candidate.probe].every(address => isIP(address) === family), "Семейства адресов шлюза не совпадают")
requireIx(!site.groups.some(entry => host(entry.gateway) === host(candidate.probe) || host(entry.gateway) === host(candidate.nextHop)), "Рекурсивный шлюз не может быть целью пробы или соседом")
requireIx(!inNetwork(sink[family].local, candidate.probe) && !inNetwork(sink[family].local, candidate.nextHop), "Путь использует служебную сеть отбрасывания")
const probe = host(candidate.probe), path = host(candidate.nextHop) + "%" + candidate.interface
requireIx(!probes.has(probe) || probes.get(probe) === path, "Контрольный адрес закреплён за несколькими путями")
probes.set(probe, path)
}
}
const exits = site.exits.map(id => groups.get(id))
requireIx(exits.every(Boolean), "Неизвестная группа обычного выхода")
for (const exit of exits) {
requireIx(exit!.onFailure === "block", "Обычный выход не может ссылаться сам на себя")
}
for (const group of site.groups) {
if (group.onFailure === "segment-exit") requireIx(exits.some(exit => familyOf(exit!.gateway) === familyOf(group.gateway)), "Для fallback нужен выход того же семейства")
}
for (const route of site.routes) {
const group = groups.get(route.group)
requireIx(group && familyOf(route.prefix) === familyOf(group.gateway), "Маршрут использует неизвестную группу или другое семейство")
requireIx(Number(route.prefix.split("/")[1]) > 0, "Default route задаётся в разделе выходов")
requireIx(!site.groups.some(entry => canonicalBgpPolicyPrefix(route.prefix) === host(entry.gateway)) && !probes.has(canonicalBgpPolicyPrefix(route.prefix)), "Префикс совпадает со служебным маршрутом")
}
for (const network of site.discardNetworks) {
requireIx(Number(network.split("/")[1]) > 0, "Маршрут защиты не может быть default route")
}
for (const peer of site.neighbors) {
const binding = bindings.get(peer.binding)
const family = familyOf(peer.address)
requireIx(binding && familyOf(peer.localAddress) === family, "BGP-сосед использует необъявленный интерфейс или другое семейство")
requireIx(peer.remoteAs !== site.autonomousSystem, "IX принимает только явно настроенных eBGP-соседей")
unique(peer.rules.map(rule => rule.community), "community у BGP-соседа")
const groupIds = peer.type === "public-feed" ? peer.rules.map(rule => rule.group) : [peer.group]
requireIx(groupIds.length && groupIds.every(id => id && groups.has(id) && familyOf(groups.get(id)!.gateway) === family), "У BGP-соседа нет подходящей группы шлюзов")
requireIx(peer.prefixes.concat(peer.exportPrefixes).every(prefix => familyOf(prefix) === family && Number(prefix.split("/")[1]) > 0), "Неверный диапазон BGP-соседа")
if (peer.type === "public-feed") {
requireIx(!peer.group && !peer.exportPrefixes.length, "Публичный фид не принимает экспорт IX")
continue
}
requireIx(peer.prefixes.length && !peer.rules.length, "Частный сосед должен объявлять согласованный диапазон")
requireIx(groups.get(peer.group!)?.onFailure === "block", "Частный префикс нельзя отправлять в обычный выход")
requireIx(peer.prefixes.every(prefix => binding.prefixes.some(allowed => inNetwork(allowed, prefix))), "BGP импортирует только диапазоны подключения")
if (peer.type === "exchange") requireIx(otherId && binding.peerServerId === otherId && binding.kind === "wireguard", "Межплощадочный BGP требует WireGuard до второй площадки")
requireIx(peer.exportPrefixes.every(prefix => ownedNetworks.some(entry => entry.binding !== binding && inNetwork(entry.prefix, prefix))), "Экспорт разрешён только из согласованных сетей других подключений")
}
}
export function compileIxConfiguration(id: string, configuration: IxConfiguration): IxRouterObject[] {
const namespace = ixNamespace(id)
const result: IxRouterObject[] = []
unique(configuration.sites.map(site => String(site.serverId)), "площадка IX")
const remoteExit = (site: IxSite, remoteId: number) => site.groups.filter(group => site.exits.includes(group.id))
.flatMap(groupPaths).some(path => site.interfaces.some(binding => binding.name === path.interface && binding.peerServerId === remoteId))
if (configuration.sites.length === 2) {
const [a, b] = configuration.sites
requireIx(!remoteExit(a, b.serverId) || !remoteExit(b, a.serverId), "Основные и резервные выходы образуют цикл между IX")
}
for (const site of configuration.sites) {
const otherId = configuration.sites.find(candidate => candidate.serverId !== site.serverId)?.serverId
validateSite(site, otherId)
const add = (path: string, key: string, fields: Record<string, string>) => {
result.push({ serverId: site.serverId, path, fields: { ...fields, comment: namespace + ":" + key } })
}
const dropInterface = ixDiscardInterface(id)
for (const [index, binding] of site.interfaces.entries()) {
const wireguard = binding.provisionWireguard
if (!wireguard) continue
add("/interface/wireguard", "wg-" + index, { name: binding.name, "listen-port": String(wireguard.listenPort) })
}
add("/interface/bridge", "drop-bridge", { name: dropInterface, "protocol-mode": "none" })
add("/ip/vrf", "vrf", { name: namespace, interfaces: [dropInterface, ...site.interfaces.map(binding => binding.name)].join(",") })
if (site.interfaces.length) {
for (const family of families) {
const filter = family === 4 ? "/ip/firewall/filter" : "/ipv6/firewall/filter"
for (const peer of site.neighbors.filter(peer => familyOf(peer.address) === family)) {
const control = { chain: "input", "in-interface": namespace, protocol: "tcp", "src-address": host(peer.address), action: "accept" }
add(filter, "allow-bgp-" + peer.id, { ...control, "dst-port": "179" })
add(filter, "bgp-reply-" + peer.id, { ...control, "src-port": "179", "connection-state": "established" })
}
add(filter, "allow-control-" + family, { chain: "input", "in-interface": namespace, protocol: family === 4 ? "icmp" : "icmpv6", action: "accept" })
add(filter, "deny-input-" + family, { chain: "input", "in-interface": namespace, action: "drop" })
add(filter, "allow-segment-" + family, { chain: "forward", "in-interface": namespace, "out-interface": namespace, action: "accept" })
add(filter, "deny-outside-" + family, { chain: "forward", "in-interface": namespace, action: "drop" })
add(filter, "deny-inside-" + family, { chain: "forward", "out-interface": namespace, action: "drop" })
}
for (const [index, binding] of site.interfaces.entries()) {
if (binding.natIpv4) add("/ip/firewall/nat", "nat-" + index, { chain: "srcnat", action: "masquerade", "out-interface": binding.name })
if (binding.kind === "gre-ipsec") {
add("/ip/firewall/filter", "gre-in-" + index, { chain: "input", protocol: "gre", "src-address": host(binding.endpoint!), "dst-address": host(binding.localEndpoint!), "ipsec-policy": "in,none", action: "drop" })
add("/ip/firewall/filter", "gre-out-" + index, { chain: "output", protocol: "gre", "src-address": host(binding.localEndpoint!), "dst-address": host(binding.endpoint!), "ipsec-policy": "out,none", action: "drop" })
}
}
}
for (const [index, binding] of site.interfaces.entries()) {
const wireguard = binding.provisionWireguard
if (!wireguard) continue
const family = familyOf(wireguard.localAddress)
add(family === 4 ? "/ip/address" : "/ipv6/address", "wg-address-" + index, {
address: wireguard.localAddress, interface: binding.name, ...(family === 6 ? { advertise: "no" } : {}),
})
add("/interface/wireguard/peers", "wg-peer-" + index, {
interface: binding.name, "public-key": wireguard.peerPublicKey,
"allowed-address": [...new Set([host(wireguard.peerAddress), ...binding.prefixes.map(canonicalBgpPolicyPrefix)])].join(","),
...(binding.endpoint ? { "endpoint-address": binding.endpoint, "endpoint-port": String(wireguard.endpointPort) } : {}),
})
}
const groups = new Map(site.groups.map(group => [group.id, group]))
const bgpGroups = new Set(site.neighbors.flatMap(peer => peer.type === "public-feed" ? peer.rules.map(rule => rule.group) : [peer.group]))
const knownProbes = new Set<string>()
const privateNetworks = [...new Set(site.discardNetworks.concat(site.neighbors.filter(peer => peer.type !== "public-feed").flatMap(peer => peer.prefixes)).map(canonicalBgpPolicyPrefix))]
for (const family of families) {
if (!site.groups.some(group => familyOf(group.gateway) === family) && !privateNetworks.some(prefix => familyOf(prefix) === family)) continue
const path = routePath(family)
const addressPath = family === 4 ? "/ip/address" : "/ipv6/address"
add(addressPath, "drop-address-" + family, { address: sink[family].local, interface: dropInterface, ...(family === 6 ? { advertise: "no" } : {}) })
const localRoute = (key: string, fields: Record<string, string>) => add(path, key, { "routing-table": namespace, ...fields })
const familyExits = site.exits.map(id => groups.get(id)!).filter(group => familyOf(group.gateway) === family)
const via = (address: string) => address + "@" + namespace
for (const group of site.groups.filter(group => familyOf(group.gateway) === family)) {
if (bgpGroups.has(group.id)) requireIx(inNetwork(bgpGatewayRange[family], group.gateway), "Используйте служебный next hop BGP из " + bgpGatewayRange[family])
const paths = groupPaths(group).concat(group.onFailure === "segment-exit" ? familyExits.flatMap(groupPaths) : [])
const distinctPaths = [...new Map(paths.map(candidate => [host(candidate.probe), candidate])).values()]
for (const [index, candidate] of distinctPaths.entries()) {
const probe = host(candidate.probe)
if (!knownProbes.has(probe)) {
localRoute("probe-" + knownProbes.size, { "dst-address": probe, gateway: candidate.nextHop + "%" + candidate.interface + "@" + namespace, scope: "10", "target-scope": "10" })
knownProbes.add(probe)
}
localRoute("gw-" + group.id + "-" + index, { "dst-address": host(group.gateway), gateway: via(candidate.probe), scope: "20", "target-scope": "11", "check-gateway": "ping", distance: String(10 + index) })
}
localRoute("gw-" + group.id + "-discard", { "dst-address": host(group.gateway), gateway: sink[family].neighbor + "%" + dropInterface + "@" + namespace, scope: "20", "target-scope": "10", distance: "254" })
// RouterOS 7.24 resolves addresses rewritten by a BGP input filter in main.
if (bgpGroups.has(group.id)) add(path, "bgp-resolve-" + group.id, {
"routing-table": "main", "dst-address": host(group.gateway), gateway: via(group.gateway), scope: "22", "target-scope": "21",
})
}
for (const [index, route] of site.routes.entries()) {
if (familyOf(route.prefix) === family) localRoute("static-" + index, { "dst-address": canonicalBgpPolicyPrefix(route.prefix), gateway: via(groups.get(route.group)!.gateway), "target-scope": "21" })
}
for (const [index, prefix] of privateNetworks.entries()) {
if (familyOf(prefix) === family) localRoute("guard-" + index, { "dst-address": prefix, blackhole: "yes", distance: "254" })
}
// Compare exit path health directly: a group's final discard next hop must never win as an ordinary exit.
for (const [index, candidate] of familyExits.flatMap(groupPaths).entries()) {
localRoute("exit-" + family + "-" + index, { "dst-address": defaultRoute[family], gateway: via(candidate.probe), "check-gateway": "ping", "target-scope": "11", distance: String(10 + index) })
}
localRoute("default-discard-" + family, { "dst-address": defaultRoute[family], blackhole: "yes", distance: "254" })
}
if (!site.neighbors.length) continue
add("/routing/bgp/instance", "bgp-instance", { name: namespace, as: String(site.autonomousSystem), "router-id": site.routerId, vrf: namespace, "routing-table": namespace })
for (const peer of site.neighbors) {
const family = familyOf(peer.address) as Family
const input = namespace + "-" + peer.id + "-in"
const output = namespace + "-" + peer.id + "-out"
const subnetCondition = (prefix: string) => "dst in " + canonicalBgpPolicyPrefix(prefix)
const setGateway = (groupId: string) => "set gw " + groups.get(groupId)!.gateway + "; set scope-target 23; accept;"
const lines = ["if (dst == " + defaultRoute[family] + ") { reject; }"]
if (peer.type === "public-feed") {
const protectedNetworks = [...forbiddenPublic[family], ...privateNetworks.filter(prefix => familyOf(prefix) === family), ...site.groups.filter(group => familyOf(group.gateway) === family).map(group => host(group.gateway)), ...knownProbes].filter(prefix => familyOf(prefix) === family)
lines.push("if (" + protectedNetworks.map(subnetCondition).join(" || ") + ") { reject; }")
const restriction = peer.prefixes.length ? " && (" + peer.prefixes.map(subnetCondition).join(" || ") + ")" : ""
for (const rule of peer.rules) lines.push("if (bgp-communities includes " + rule.community + restriction + ") { " + setGateway(rule.group) + " }")
} else {
lines.push("if (" + peer.prefixes.map(subnetCondition).join(" || ") + ") { " + setGateway(peer.group!) + " }")
}
const exportRules = peer.exportPrefixes.length ? "if (" + peer.exportPrefixes.map(subnetCondition).join(" || ") + ") { accept; } reject;" : "reject;"
add("/routing/filter/rule", "bgp-in-" + peer.id, { chain: input, rule: lines.concat("reject;").join("\n") })
add("/routing/filter/rule", "bgp-out-" + peer.id, { chain: output, rule: exportRules })
add("/routing/bgp/connection", "neighbor-" + peer.id, {
name: namespace + "-" + peer.id, instance: namespace, afi: familyName(family),
"local.role": "ebgp", "local.address": peer.localAddress, "remote.address": host(peer.address), "remote.as": String(peer.remoteAs),
"routing-table": namespace, vrf: namespace, "input.filter": input, "output.filter-chain": output,
["input.limit-process-routes-ipv" + family]: String(peer.limit),
...(peer.type === "exchange" ? { multihop: "yes", "output.redistribute": "static" } : {}),
})
}
}
requireIx(result.length <= 500, "План IX превышает 500 объектов; разделите конфигурацию")
return result
}
@@ -0,0 +1,92 @@
import type { PoolClient } from "pg"
import { ixSegmentConfigurationSchema, type IxConfiguration, type IxSegment } from "@mmapp/contracts/internet-exchange"
import { pool } from "../db/index.js"
import { IxConflict, ixDeviceIds } from "./internet-exchange-plan.js"
import { admitPartitionWriter } from "./partition-write-transaction.js"
interface SegmentRow {
id: string
revision: number
applied_revision: number | null
configuration: unknown
updated_at: Date
}
const columns = "id,revision,applied_revision,configuration,updated_at"
function segment(row: SegmentRow): IxSegment {
return { id: row.id, revision: row.revision, appliedRevision: row.applied_revision,
configuration: ixSegmentConfigurationSchema.parse(row.configuration), updatedAt: row.updated_at.toISOString() }
}
export async function listIxSegments(limit: number, offset: number, allowedIds?: readonly number[]) {
const { rows } = await pool.query<SegmentRow>(
`SELECT ${columns} FROM ix_segments s
WHERE $1::bigint[] IS NULL OR NOT EXISTS (
SELECT 1 FROM ix_segment_devices d WHERE d.segment_id=s.id AND NOT (d.server_id=ANY($1::bigint[]))
)
ORDER BY name,id LIMIT $2 OFFSET $3`, [allowedIds ?? null, limit, offset])
return rows.map(segment)
}
export async function readIxSegment(id: string, sql: Pick<PoolClient, "query"> = pool) {
const { rows: [row] } = await sql.query<SegmentRow>(`SELECT ${columns} FROM ix_segments WHERE id=$1`, [id])
return row ? segment(row) : null
}
export async function readIxRevision(id: string, revision: number) {
const { rows: [row] } = await pool.query("SELECT configuration FROM ix_segment_revisions WHERE segment_id=$1 AND revision=$2", [id, revision])
if (!row) throw new IxConflict("Ревизия IX не найдена", 404)
return ixSegmentConfigurationSchema.parse(row.configuration)
}
export async function assertIxName(id: string, name: string) {
const { rows } = await pool.query("SELECT 1 FROM ix_segments WHERE name=$1 AND id<>$2", [name, id])
if (rows.length) throw new IxConflict("Имя IX уже занято")
}
export async function assertIxSites(configuration: IxConfiguration, sql: Pick<PoolClient, "query"> = pool) {
const ids = ixDeviceIds(configuration)
const { rows } = await sql.query<{ id: number; type: string; enabled: boolean }>(
"SELECT id,type,enabled FROM servers WHERE id=ANY($1::bigint[])", [ids])
if (ids.length !== rows.length || rows.some(row => !row.enabled) || configuration.sites.some(site => rows.find(row => row.id === site.serverId)?.type !== "ix-node")) {
throw new IxConflict("Площадки должны привязываться к включённым серверам типа IX Node, а все соседние устройства должны быть доступны")
}
}
export function assertIxRevision(current: IxSegment | null, expected: number) {
if ((current?.revision ?? 0) !== expected) throw new IxConflict("Ревизия IX изменилась. Обновите данные и подготовьте новый план")
}
export async function writeIxRevision(id: string, expected: number, configuration: IxConfiguration, actor: string, applied: boolean) {
const client = await pool.connect()
try {
await client.query("BEGIN")
await admitPartitionWriter(client)
await assertIxSites(configuration, client)
const revision = expected + 1
const { rows: [row] } = expected === 0
? await client.query<SegmentRow>(`INSERT INTO ix_segments(id,name,revision,configuration,applied_revision)
VALUES ($1,$2,$3,$4::jsonb,$5) ON CONFLICT DO NOTHING RETURNING ${columns}`,
[id, configuration.name, revision, JSON.stringify(configuration), applied ? revision : null])
: await client.query<SegmentRow>(`UPDATE ix_segments SET name=$2,revision=$3,configuration=$4::jsonb,
applied_revision=CASE WHEN $5::boolean THEN $3 ELSE applied_revision END,updated_at=now()
WHERE id=$1 AND revision=$6 RETURNING ${columns}`,
[id, configuration.name, revision, JSON.stringify(configuration), applied, expected])
if (!row) throw new IxConflict("Имя IX уже занято или ревизия сменилась")
// Keep references to the applied topology while it can still hold live resources.
const appliedConfiguration = !applied && row.applied_revision !== null
? (await client.query("SELECT configuration FROM ix_segment_revisions WHERE segment_id=$1 AND revision=$2", [id, row.applied_revision])).rows[0]?.configuration
: null
const ids = [...new Set(ixDeviceIds(configuration).concat(appliedConfiguration ? ixDeviceIds(ixSegmentConfigurationSchema.parse(appliedConfiguration)) : []))]
await client.query("DELETE FROM ix_segment_devices WHERE segment_id=$1", [id])
await client.query("INSERT INTO ix_segment_devices(segment_id,server_id) SELECT $1, unnest($2::bigint[])", [id, ids])
await client.query("INSERT INTO ix_segment_revisions(segment_id,revision,configuration,actor_id) VALUES ($1,$2,$3::jsonb,$4)",
[id, revision, JSON.stringify(configuration), actor])
await client.query("COMMIT")
return segment(row)
} catch (error) {
await client.query("ROLLBACK").catch(() => {})
if (error && typeof error === "object" && "code" in error && error.code === "23505") throw new IxConflict("Имя IX уже занято")
throw error
} finally { client.release() }
}
+22 -1
View File
@@ -1,5 +1,5 @@
import assert from "node:assert/strict"
import { readOspfRouteProof } from "./ospf-route-proof.js"
import { readOspfRouteAbsence, readOspfRouteProof } from "./ospf-route-proof.js"
import { chrFixtureVrf } from "../modules/mcp/chr-fixture-context.js"
const selection = { prefix: "198.19.1.2/32", table: "local-vrf", interface: "owned-gre" }
@@ -10,6 +10,27 @@ const client = { get: async () => rows, post: async (_path: string, body: Record
const proof = await readOspfRouteProof(client, selection)
assert.equal(proof.metric, 60); assert.equal(JSON.stringify(proof).includes("NEVER_"), false)
const original = structuredClone(rows)
const largeRib = Array.from({ length: 12_608 }, (_, index) => ({
".id": `*r${index.toString(16)}`, "dst-address": `10.${index >> 8}.${index & 255}.0/24`, "routing-table": selection.table,
}))
const exactSelection = { ...selection, table: "test & vrf", interface: "wg test" }
const largeTableClient = {
get: async (path: string) => {
const url = new URL(path, "http://fixture.invalid")
if (url.pathname === "/routing/table") return [{ ".id": "*t", name: exactSelection.table }]
assert.equal(url.pathname, "/routing/route")
assert.equal(url.searchParams.get("routing-table"), exactSelection.table)
const prefix = url.searchParams.get("dst-address")
assert(prefix)
return [...largeRib, { ...original[0], "routing-table": exactSelection.table, "immediate-gw": exactSelection.interface }]
.filter(row => row["routing-table"] === exactSelection.table && row["dst-address"] === prefix)
},
post: async () => { throw new Error("unexpected getter") },
} as unknown as Parameters<typeof readOspfRouteProof>[0]
assert.equal((await readOspfRouteProof(largeTableClient, exactSelection)).interface, "wg test")
await assert.rejects(readOspfRouteProof(largeTableClient, { ...exactSelection, interface: "wg" }), /selected interface/)
await assert.rejects(readOspfRouteAbsence(largeTableClient, exactSelection, async () => {}), /matching route/)
assert.equal((await readOspfRouteAbsence(largeTableClient, { ...exactSelection, prefix: "198.19.1.3/32" }, async () => {})).absenceVerified, true)
rows = [{ ...original[0], ospf: "", active: "", "ospf.metric": "" }]
getters = []; assert.equal((await readOspfRouteProof(client, selection)).metric, 40); assert.deepEqual(getters, ["ospf", "active", "ospf.metric"])
for (const patch of [{ ospf: "false" }, { active: "false" }, { contribution: "unreachable" }, { "ospf.metric": "unknown" }, { "ospf.metric": "16777216" }, { "immediate-gw": "198.18.1.2%owned-gre-other" }, { "immediate-gw": "198.18.1.2%owned-gre@foreign-vrf" }]) {
+13 -3
View File
@@ -25,10 +25,18 @@ async function readBoundedRows(client: Pick<MikrotikClient, "get">, path: string
return rows
}
function routeLookupPath(prefix: string, table: string, includeScopedDestinations = false) {
const properties = ".id,dst-address,routing-table,afi,ospf,active,ospf.metric,immediate-gw,contribution"
const destination = includeScopedDestinations ? "" : `&dst-address=${encodeURIComponent(prefix)}`
return `/routing/route?.proplist=${properties}&routing-table=${encodeURIComponent(table)}${destination}`
}
export async function readOspfRouteAbsence(client: Pick<MikrotikClient, "get">, selection: { prefix: string; table: string }, beforeRead: () => Promise<unknown>) {
const prefix = canonicalOspfRouteProofPrefix(selection.prefix)
if (!prefix || !selection.table || selection.table.length > 200) throw new OspfRouteProofError("Invalid OSPF route absence context")
const routes = await readBoundedRows(client, "/routing/route", beforeRead)
// RouterOS can render link-local destinations as fe80::%interface/64.
const linkLocal = /^fe[89ab][0-9a-f]:/i.test(prefix)
const routes = await readBoundedRows(client, routeLookupPath(prefix, selection.table, linkLocal), beforeRead)
for (const row of routes) {
const observed = row["dst-address"]
const scoped = typeof observed === "string" ? /^([0-9a-f:]+)%[^/%]+(\/\d+)$/i.exec(observed) : null
@@ -52,7 +60,7 @@ export async function readOspfRouteProof(client: Pick<MikrotikClient, "get" | "p
const prefix = canonicalOspfRouteProofPrefix(selection.prefix)
if (!prefix || !selection.table || !selection.interface || selection.table.length > 200 || selection.interface.length > 200) throw new OspfRouteProofError("Invalid OSPF route proof context")
const family = isIP(prefix.split("/")[0])
const rows = await readBoundedRows(client, "/routing/route", beforeRead)
const rows = await readBoundedRows(client, routeLookupPath(prefix, selection.table), beforeRead)
const matches = rows.filter(row => {
if (row["routing-table"] !== selection.table || canonicalOspfRouteProofPrefix(row["dst-address"]) !== prefix) return false
const active = flag(row.active)
@@ -74,7 +82,9 @@ export async function readOspfRouteProof(client: Pick<MikrotikClient, "get" | "p
if (metric === null) throw new OspfRouteProofError("OSPF route metric not confirmed")
const gateway = typeof row["immediate-gw"] === "string" ? row["immediate-gw"] : ""
const context = /^([^%@]+)%([^@]+)(?:@([^@]+))?$/.exec(gateway)
if (!context || isIP(context[1]) !== family || context[2] !== selection.interface || context[3] && context[3] !== selection.table) throw new OspfRouteProofError("OSPF route does not resolve through the selected interface/VRF")
const directInterface = gateway === selection.interface && !isIP(gateway)
const scopedInterface = context && isIP(context[1]) === family && context[2] === selection.interface && (!context[3] || context[3] === selection.table)
if (!directInterface && !scopedInterface) throw new OspfRouteProofError("OSPF route does not resolve through the selected interface/VRF")
await beforeRead()
return { id: String(row[".id"]), family: family === 4 ? "ipv4" : "ipv6", prefix: selection.prefix, table: selection.table, interface: selection.interface, immediateGateway: gateway, ospf: true, active: true, metric }
}
@@ -103,9 +103,27 @@ for (const type of ["A", "AAAA", "MX", "NS", "TXT", "CNAME", "PTR"]) {
{
const f = fixture(); f.raw([]); await failure(f.executor.execute(await f.executor.prepare(1, request("mtu"))), "MEASUREMENT_UNKNOWN"); assert.equal(f.counts().sends, 1)
}
for (const host of ["192.0.2.1", "router.example"]) {
const f = fixture()
f.source.host = host
f.raw(["192.0.2.1"])
const post = f.deps.post
f.deps.post = async (...args) => {
const source = args[2]["src-address"] ?? "10.205.1.2"
f.raw(source === "10.205.1.2"
? [{ seq: "0", ttl: "64", time: "25ms", sent: "1", received: "1", "packet-loss": "0" }]
: [{ seq: "0", status: "timeout", sent: "1", received: "0", "packet-loss": "100" }])
return post(...args)
}
const prepared = await f.executor.prepare(1, request("ping", { target: "10.205.1.1", pingCount: 1 }))
const result = await f.executor.execute(prepared)
assert.match(result.output!, /sent=1 received=1 packet-loss=0/)
assert.deepEqual(f.counts(), { sends: 1, dnsCalls: 0 })
cases++
}
{
const f = fixture(); f.source.host = "router.example"; f.sourceFailure()
const prepared = await f.executor.prepare(1, request("ping")), result = await f.executor.execute(prepared)
const prepared = await f.executor.prepare(1, request("traceroute")), result = await f.executor.execute(prepared)
assert.equal(result.state, "completed"); assert.equal(f.counts().dnsCalls, 1); assert.equal(f.counts().sends, 1)
assert.equal((f.events.find(event => event.kind === "native")!.value as { body: Record<string, string> }).body["src-address"], undefined)
assert.equal(f.events.filter(event => event.kind === "received").length, 2); cases++
@@ -137,7 +155,7 @@ for (const kind of ["claim", "intent", "receipt", "received"] as const) {
}
{
const f = fixture(); f.source.host = "router.example"; f.raw(["192.0.2.1"])
const prepared = await f.executor.prepare(1, request("ping")); f.lost("received")
const prepared = await f.executor.prepare(1, request("traceroute")); f.lost("received")
await failure(f.executor.execute(prepared), "JOURNAL_UNKNOWN"); assert.equal(f.counts().dnsCalls, 1); assert.equal(f.counts().sends, 0); assert.equal(f.events.filter(event => event.kind === "received").length, 1)
}
{
@@ -232,7 +232,8 @@ export function createServerProbeExecutor(deps: ServerProbeExecutionDependencies
}
claimed = true
let sourceIpv4: string | null = null
if (["ping", "traceroute", "mtu"].includes(plan.request.tool)) {
// Ping must let RouterOS choose the source for the route, including WireGuard.
if (plan.request.tool === "traceroute" || plan.request.tool === "mtu") {
const host = currentSource!.host.trim()
if (isIPv4(host)) sourceIpv4 = host
else {
@@ -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)
+57 -63
View File
@@ -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),
@@ -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" }
+9 -10
View File
@@ -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 extends FlowTupleRow>(
): T[] {
const byConv = new Map<string, T>()
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<T extends FlowTupleRow>(
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<T extends FlowTupleRow>(rows: T[]): T[] {
const byIface = new Map<string, T>()
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<string, T>()
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)
}
+6 -5
View File
@@ -53,17 +53,16 @@ export function destCtxForIface(
topo: FlowTopology | null | undefined,
serverId: number,
inIface: string,
nat?: Pick<InternetDestCtx, "natSrc" | "natDst" | "natSrcPort" | "natDstPort">,
nat?: Pick<InternetDestCtx, "natSrc" | "natDst" | "natSrcPort" | "natDstPort"> & { 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,
+1 -1
View File
@@ -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<string, string>()
let interfaceUncertain = false
@@ -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,
@@ -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()
+27 -12
View File
@@ -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)
@@ -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{
@@ -3,12 +3,39 @@ import { EventEmitter } from "node:events"
import http from "node:http"
import { test, type TestContext } from "node:test"
import { isRouterOsPreHeaderGetReset, MikrotikClient } from "./mikrotik.js"
import { createWireguardDependencyReader } from "./wireguard-interface-delete.js"
import { createWireguardDependencyReader, readWireguardRouteDependencies } from "./wireguard-interface-delete.js"
import { assertGreDeleteDependencies } from "./gre-mutation-state.js"
import { wireguardReadDiagnostic } from "./wireguard-read-diagnostic.js"
type Outcome = "reset" | "late-reset" | "informational-reset" | "body-reset" | "ok" | "refused" | "invalid-json"
const reset = () => Object.assign(new Error("fixture reset"), { code: "ECONNRESET" })
test("large routing tables do not enter a new WireGuard snapshot and still protect existing interfaces", async () => {
const target = { ".id": "*a", name: "wg-new" }
const configured = { ".id": "*b", static: "true", gateway: "wg-new-backup" }
const learned = Array.from({ length: 12_608 }, (_, index) => ({
".id": `*r${index.toString(16)}`, dynamic: "true", "immediate-gw": "192.0.2.1%ether1",
}))
const requests: string[] = []
const read: ReturnType<typeof createWireguardDependencyReader> = async (path, query) => {
requests.push(path + query)
return query === "?static=true" ? [configured] : learned
}
const path = "/ip/route"
assert.deepEqual(await readWireguardRouteDependencies(read, path, false), [configured])
assert.deepEqual(requests, ["/ip/route?static=true"])
const assertSafe = (routes: Record<string, string>[]) => assertGreDeleteDependencies({ dependencies: { [path]: routes }, bindings: [], filters: [] }, target)
assertSafe(await readWireguardRouteDependencies(read, path, true))
assert.match(requests.at(-1)!, /^\/ip\/route\?dynamic=true&\.proplist=.*immediate-gw/)
configured.gateway = target.name
assert.throws(() => assertSafe([configured]), /используется/)
configured.gateway = "ether1"
learned[12_607]["immediate-gw"] = "192.0.2.1%wg-new"
const routes = await readWireguardRouteDependencies(read, path, true)
assert.throws(() => assertSafe(routes), /используется/)
await assert.rejects(readWireguardRouteDependencies(async () => learned, path, false), { code: "STATE_UNAVAILABLE" })
})
function fixture(t: TestContext, outcomes: Outcome[]) {
const paths: string[] = [], timeouts: number[] = []
t.mock.method(http, "request", (options: http.RequestOptions, receive: (res: unknown) => void) => {
@@ -41,22 +41,22 @@ async function source(server: ServerRow, authorize: () => Promise<unknown>) {
const fresh = await getServerRowById(server.id)
if (!fresh || !sameWireguardEndpoint(fresh, server)) throw fail("STATE_CONFLICT", "Источник WireGuard изменился")
}
function nativeRows(raw: unknown, required: string[] = []): Row[] {
if (!Array.isArray(raw) || raw.length > 2000 || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.values(row).some(value => typeof value !== "string") || typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"]) || required.some(key => !row[key]?.trim()))) throw fail("STATE_UNAVAILABLE", "Недостоверный каталог зависимостей WireGuard")
function nativeRows(raw: unknown, required: string[] = [], rowLimit = 2000): Row[] {
if (!Array.isArray(raw) || raw.length > rowLimit || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.values(row).some(value => typeof value !== "string") || typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"]) || required.some(key => !row[key]?.trim()))) throw fail("STATE_UNAVAILABLE", "Недостоверный каталог зависимостей WireGuard")
if (new Set(raw.map(row => row[".id"])).size !== raw.length) throw fail("STATE_UNAVAILABLE", "Повторяющиеся ID в каталоге WireGuard")
return raw as Row[]
}
/** Retry at most two distinct catalogue GETs per capture, with fresh source authority. */
export function createWireguardDependencyReader(client: Pick<MikrotikClient, "get">, authorizeSource: () => Promise<unknown>, now = () => performance.now()) {
const timeoutMs = 10_000, maxBytes = 1_048_576
const timeoutMs = 10_000
const unavailable = () => fail("STATE_UNAVAILABLE", "Не прочитан обязательный источник зависимостей WireGuard")
const retriedPaths = new Set<string>()
return async (path: string): Promise<unknown> => {
return async (path: string, query = "", maxBytes = 1_048_576): Promise<unknown> => {
await authorizeSource()
const deadline = now() + timeoutMs
try {
return await client.get(path, timeoutMs, undefined, maxBytes)
return await client.get(path + query, timeoutMs, undefined, maxBytes)
} catch (error) {
const retryableCatalogue = path === "/ip/address" || path === "/ipv6/address" || wireguardInterfaceDependencyPaths.includes(path)
if (retriedPaths.size >= 2 || retriedPaths.has(path) || !retryableCatalogue
@@ -67,14 +67,40 @@ export function createWireguardDependencyReader(client: Pick<MikrotikClient, "ge
const remaining = Math.floor(deadline - now())
if (remaining < 1) throw markWireguardReadFailure(unavailable(), path, 2, "read-deadline")
try {
return await client.get(path, remaining, undefined, maxBytes)
return await client.get(path + query, remaining, undefined, maxBytes)
} catch (error) {
throw markWireguardGetFailure(unavailable(), path, 2, error)
}
}
}
export async function readWireguardInterfaceDeleteState(server: ServerRow, id: string, authorize: () => Promise<unknown> = async () => {}): Promise<WireguardInterfaceDeleteState> {
const routePaths = new Set(["/ip/route", "/ipv6/route"])
const routeRowLimit = 50_000
const routeByteLimit = 8 * 1024 * 1024
export async function readWireguardRouteDependencies(read: ReturnType<typeof createWireguardDependencyReader>, path: string, existingInterface: boolean) {
const catalogue = async (query: string, dynamic: boolean) => {
const raw = await read(path, query, routeByteLimit)
if (Array.isArray(raw) && raw.length > routeRowLimit) throw markWireguardReadFailure(fail("STATE_UNAVAILABLE", "Превышен предел отфильтрованного каталога маршрутов WireGuard"), path, undefined, "catalogue-limit")
try {
const rows = nativeRows(raw, [], routeRowLimit)
if (rows.some(row => dynamic ? observedBoolean(row.dynamic) !== true : observedBoolean(row.dynamic) === true || observedBoolean(row.static) === false)) throw fail("STATE_UNAVAILABLE", "RouterOS вернул маршруты вне запрошенного фильтра")
return rows
} catch (error) {
if (!(error instanceof McpError)) throw error
throw markWireguardReadFailure(error, path, undefined, "invalid-catalogue")
}
}
const configured = await catalogue("?static=true", false)
if (!existingInterface) return configured
// Preserve checks on learned next hops before editing or removing an existing interface.
const learned = await catalogue("?dynamic=true&.proplist=.id,dynamic,connect,belongs-to,gateway,immediate-gw,vrf-interface,interface,local-address,routing-table", true)
const configuredIds = new Set(configured.map(row => row[".id"]))
if (learned.some(row => configuredIds.has(row[".id"]))) throw markWireguardReadFailure(fail("STATE_UNAVAILABLE", "Маршрут изменился во время сверки зависимостей WireGuard"), path, undefined, "invalid-catalogue")
return [...configured, ...learned]
}
export async function readWireguardInterfaceDeleteState(server: ServerRow, id: string, authorize: () => Promise<unknown> = async () => {}, includeLearnedRoutes?: boolean): Promise<WireguardInterfaceDeleteState> {
const base = await readWireguardPeerDeletionState(server, authorize), client = MikrotikClient.fromServer(server)
const readDependency = createWireguardDependencyReader(client, () => source(server, authorize))
let totalRows = base.interfaces.length + base.peers.length, bytes = Buffer.byteLength(JSON.stringify([base.interfaces, base.peers]))
@@ -93,8 +119,10 @@ export async function readWireguardInterfaceDeleteState(server: ServerRow, id: s
}
const ipv4 = await read("/ip/address", ["address", "interface"]), ipv6 = await read("/ipv6/address", ["address", "interface"])
const dependencies: Record<string, Row[]> = {}
for (const path of wireguardInterfaceDependencyPaths) dependencies[path] = await read(path, path === "/interface" ? ["name"] : [])
const parent = base.interfaces.find(row => row[".id"] === id)
for (const path of wireguardInterfaceDependencyPaths) dependencies[path] = routePaths.has(path)
? await readWireguardRouteDependencies(readDependency, path, includeLearnedRoutes ?? (parent !== undefined))
: await read(path, path === "/interface" ? ["name"] : [])
if (parent && [...ipv4, ...ipv6].filter(row => [parent.name, id].includes(row.interface) && !automaticLinkLocal(row)).length + base.peers.filter(row => [parent.name, id].includes(row.interface)).length > 49) throw fail("STATE_UNAVAILABLE", "Каскад WireGuard превышает предел зависимых объектов")
if (parent) for (const [path, rows] of [["/ip/address", ipv4], ["/ipv6/address", ipv6]] as const) for (const row of rows.filter(row => [parent.name, id].includes(row.interface))) {
try {
@@ -69,11 +69,11 @@ export const wireguardRestoreStateProof=(state:WireguardRestoreState)=>wireguard
export async function readWireguardRestoreState(server:ServerRow,beforeRead:()=>Promise<unknown>=async()=>{}) {
const deadline=Date.now()+45000
const authorize=async()=>{if(Date.now()>=deadline)throw fail("Истёк бюджет чтения WireGuard restore","STATE_UNAVAILABLE");await beforeRead();if(Date.now()>=deadline)throw fail("Истёк бюджет чтения WireGuard restore","STATE_UNAVAILABLE")}
const state=await readWireguardInterfaceDeleteState(server,"*0",authorize)
const state=await readWireguardInterfaceDeleteState(server,"*0",authorize,true)
if(new Set(state.base.interfaces.map(row=>row.name)).size!==state.base.interfaces.length||state.base.interfaces.some(row=>!row.name)||state.base.peers.some(peer=>state.base.interfaces.filter(row=>row.name===peer.interface||row[".id"]===peer.interface).length!==1))throw fail("Неоднозначные родители WireGuard")
// Selected-parent reads corroborate dynamic origins; the *0 inventory alone does not.
for(const parent of state.base.interfaces){
const selected=await readWireguardInterfaceDeleteState(server,parent[".id"],authorize),comparison=structuredClone(selected)
const selected=await readWireguardInterfaceDeleteState(server,parent[".id"],authorize,false),comparison=structuredClone(selected)
for(const resource of ["ipv4","ipv6"]as const)for(const row of comparison[resource]){const previous=state[resource].find(a=>a[".id"]===row[".id"]);if(previous?.dynamic===undefined)delete row.dynamic;else row.dynamic??=previous.dynamic}
if(await wireguardRestoreStateProof(comparison)!==await wireguardRestoreStateProof(state))throw fail("WireGuard изменился во время инвентаризации")
for(const resource of ["ipv4","ipv6"]as const)for(const row of state[resource]){const observed=selected[resource].find(a=>a[".id"]===row[".id"]&&a.interface===row.interface);if(observed?.dynamic!==undefined)row.dynamic=observed.dynamic}
@@ -48,12 +48,14 @@ const TYPE_LABELS: Record<ServerType, string> = {
"jump-host": "JumpHost",
"exit-node": "Exit Node",
"home-router": "Home Router",
"ix-node": "IX Node",
}
const TYPE_VARIANTS: Record<ServerType, BadgeProps["variant"]> = {
"jump-host": "primary-light",
"exit-node": "info-light",
"home-router": "success-light",
"ix-node": "info-light",
}
function rosVer(os: string): number {
@@ -46,7 +46,7 @@ export interface UptimeResourceRow {
temp?: number
}
function TypeChip({ type }: { type: "jump-host" | "exit-node" | "home-router" }) {
function TypeChip({ type }: { type: import("@mmapp/contracts/servers").ServerType }) {
return (
<span className={cn(
"inline-flex items-center rounded px-1.5 py-0.5 text-[10px] font-bold border shrink-0",
@@ -56,7 +56,7 @@ function TypeChip({ type }: { type: "jump-host" | "exit-node" | "home-router" })
? "bg-violet-500/10 text-violet-600 dark:text-violet-400 border-violet-500/20"
: "bg-sky-500/10 text-sky-600 dark:text-sky-400 border-sky-500/20",
)}>
{type === "jump-host" ? "JH" : type === "home-router" ? "HR" : "EN"}
{type === "jump-host" ? "JH" : type === "home-router" ? "HR" : type === "ix-node" ? "IX" : "EN"}
</span>
)
}
+13 -6
View File
@@ -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<McpConnection["policy"]["mode"]>(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<string[]>(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
<OperationChoices operations={eligible} selected={selected} onChange={setSelected} />
<p className="text-xs text-muted-foreground">Выбор операций учитывает права владельца. Режим, список серверов и глобальный доступ задаются отдельно. Новые инструменты нужно добавлять вручную. Для применения планов выберите также инструменты просмотра и применения.</p>
</>}
{step === 3 && <><div className="grid gap-3 sm:grid-cols-2">{Object.entries(limits).map(([key, value]) => <Field key={key} label={({ callsPerMinute: "Вызовов в минуту", concurrentReads: "Одновременных чтений", mutationsPerHour: "Изменений в час", serversPerPlan: "Серверов на план", objectsPerPlan: "Объектов на план" } as Record<string, string>)[key]}><Input type="number" min={1} value={value} onChange={event => setLimits(current => ({ ...current, [key]: Number(event.target.value) }))} /></Field>)}</div>{service ? <>
{step === 3 && <>
{service && <Choice label="Отключить квоты частоты для этого ключа" checked={unlimitedRates} onChange={setUnlimitedRates} />}
{ratesDisabled && <p className="text-sm text-muted-foreground">Вызовы, одновременные чтения и изменения в час без квот подключения. Общая ёмкость сервера и размер плана ограничены.</p>}
<div className="grid gap-3 sm:grid-cols-2">{Object.entries(limits).filter(([key]) => !ratesDisabled || key === "serversPerPlan" || key === "objectsPerPlan").map(([key, value]) => <Field key={key} label={({ callsPerMinute: "Вызовов в минуту", concurrentReads: "Одновременных чтений", mutationsPerHour: "Изменений в час", serversPerPlan: "Серверов на план", objectsPerPlan: "Объектов на план" } as Record<string, string>)[key]}><Input type="number" min={1} value={value} onChange={event => setLimits(current => ({ ...current, [key]: Number(event.target.value) }))} /></Field>)}</div>{service ? <>
<Choice label="Выполнять планы без ручного подтверждения" checked={unattended} onChange={setUnattended} />
<p className="text-sm text-muted-foreground">Действует для всех операций, выданных ключу. Клиент должен сначала показать точный план в чате. Сервер проверяет контрольную сумму плана, а публикацию сообщения обеспечивает клиент. Защита объектов сохраняется.</p>
</> : <><NativeSelect label="Опасные действия" value={danger} onChange={value => setDanger(value as typeof danger)}><option value="approval">Всегда подтверждать</option><option value="explicit-allow">Автономно для явно выбранных действий</option></NativeSelect>{danger === "explicit-allow" && <>
<p className="text-sm text-muted-foreground">В автономном режиме выбранные здесь опасные операции смогут выполняться без подтверждения, если этого не требует защита объекта или сама операция.</p>
<OperationChoices operations={dangerous} selected={autonomousDangerIds} onChange={setDangerIds} selectAllLabel="Выбрать все опасные операции" />
</>}</>}</>}
{step === 4 && <div className="space-y-3 text-sm"><p><strong>{name || "Без названия"}</strong> · {service ? "Сервисное, бессрочное" : modeLabels[mode]}</p><p>Серверы: {selectAllServers ? "все на момент сохранения" : serverIds.join(", ") || "не выбраны"}. Операций: {selected.length}. Глобальный доступ: {global ? "да" : "нет"}.</p>{service && unattended && <p>Ручное подтверждение планов отключено. Клиент публикует план в чате перед применением.</p>}{!service && mode === "autonomous" && danger === "explicit-allow" && <p>Опасных операций выбрано для автономного выполнения: {autonomousDangerIds.length}.</p>}<p>Защита объектов продолжает действовать независимо от автономных разрешений.</p><p>После сохранения проверьте конкретное действие в симуляторе на вкладке «Разрешения».</p></div>}
{step === 4 && <div className="space-y-3 text-sm"><p><strong>{name || "Без названия"}</strong> · {service ? "Сервисное, бессрочное" : modeLabels[mode]}</p><p>Серверы: {selectAllServers ? "все на момент сохранения" : serverIds.join(", ") || "не выбраны"}. Операций: {selected.length}. Глобальный доступ: {global ? "да" : "нет"}.</p>{ratesDisabled && <p>Квоты частоты для этого ключа отключены.</p>}{service && unattended && <p>Ручное подтверждение планов отключено. Клиент публикует план в чате перед применением.</p>}{!service && mode === "autonomous" && danger === "explicit-allow" && <p>Опасных операций выбрано для автономного выполнения: {autonomousDangerIds.length}.</p>}<p>Защита объектов продолжает действовать независимо от автономных разрешений.</p><p>После сохранения проверьте конкретное действие в симуляторе на вкладке «Разрешения».</p></div>}
<div className="flex flex-wrap justify-between gap-2"><Button variant="outline" disabled={busy} onClick={() => step > 1 ? setStep(step - 1) : close()}>{step > 1 ? "Назад" : "Отмена"}</Button>{step < 4 ? <Button onClick={() => setStep(step + 1)}>Далее</Button> : <Button disabled={busy} onClick={() => { void save() }}>{busy ? "Сохранение…" : "Сохранить доступ"}</Button>}</div>
</DialogContent></Dialog>
}
@@ -819,7 +826,7 @@ function Settings({ api }: { api: McpApi }) {
}
function RestCoverage({ api }: { api: McpApi }) {
const query = useQuery(useCallback(signal => api.coverage(signal), [api])), routes = query.data?.routes ?? []
return <Panel title="Покрытие REST через MCP" description="Сервисные сценарии проверяются отдельно от количества инструментов. Новые endpoints отслеживает автоматическая проверка реестра."><ErrorMessage message={query.error} /><div className="flex flex-wrap gap-2"><Badge variant="success-light">Сценариев с MCP: {routes.filter(route => route.status === "mediated").length}</Badge><Badge variant="info-light">Транспорт и управление доступом: {routes.filter(route => route.status === "excluded").length}</Badge><Badge variant="warning-light">Ожидают реализации: {routes.filter(route => route.status === "pending").length}</Badge></div><Grid items={routes.filter(route => route.status === "pending")} columns={[{ accessorKey: "method", header: "Метод", size: 90 }, { accessorKey: "path", header: "Незавершённый сценарий", size: 310 }, { accessorKey: "reason", header: "Что осталось", size: 400 }]} loading={!query.data && !query.error} /></Panel>
return <Panel title="Покрытие REST через MCP" description="Сервисные сценарии проверяются отдельно от количества инструментов. Новые endpoints отслеживает автоматическая проверка реестра."><ErrorMessage message={query.error} /><div className="flex flex-wrap gap-2"><Badge variant="success-light">Сценариев с MCP: {routes.filter(route => route.status === "mediated").length}</Badge><Badge variant="info-light">Служебные маршруты: {routes.filter(route => route.status === "excluded").length}</Badge><Badge variant="warning-light">Ожидают реализации: {routes.filter(route => route.status === "pending").length}</Badge></div><Grid items={routes.filter(route => route.status === "pending")} columns={[{ accessorKey: "method", header: "Метод", size: 90 }, { accessorKey: "path", header: "Незавершённый сценарий", size: 310 }, { accessorKey: "reason", header: "Что осталось", size: 400 }]} loading={!query.data && !query.error} /></Panel>
}
function Consent({ api, id }: { api: McpApi; id: string }) {
const request = useQuery(useCallback(signal => api.consentRequest(id, signal), [api, id])), connections = useQuery(useCallback(signal => api.connections(signal), [api]))
+1 -1
View File
@@ -66,6 +66,6 @@ export function PolicyPreviewDecision({ result }: { result: McpPolicyDecision })
return <div role="status" className="flex flex-col gap-3 rounded-md border p-3 text-sm">
<Badge variant={result.decision === "allowed" ? "success-light" : result.decision === "denied" ? "destructive-light" : "warning-light"}>{result.decision === "allowed" ? "Разрешено" : result.decision === "denied" ? "Запрещено" : "Нужно подтверждение"}</Badge>
{result.reasons.length > 0 && <ul className="list-inside list-disc space-y-1">{result.reasons.map((item, index) => <li key={`${item.code}:${index}`}>{item.message} <span className="text-xs text-muted-foreground">({item.code})</span></li>)}</ul>}
<p className="text-xs text-muted-foreground">Лимиты подключения: {result.limits.callsPerMinute} вызовов/мин · {result.limits.concurrentReads} параллельных чтения · {result.limits.mutationsPerHour} изменений/ч · до {result.limits.serversPerPlan} серверов и {result.limits.objectsPerPlan} объектов в плане.</p>
<p className="text-xs text-muted-foreground">{result.unlimitedRates ? "Квоты частоты отключены" : `${result.limits.callsPerMinute} вызовов/мин · ${result.limits.concurrentReads} одновременных чтений · ${result.limits.mutationsPerHour} изменений/ч`} · до {result.limits.serversPerPlan} серверов и {result.limits.objectsPerPlan} объектов в плане.</p>
</div>
}
+1 -1
View File
@@ -22,7 +22,7 @@ export function NetworkMapLegend() {
</div>
<div className="flex flex-col gap-1.5">
<p className="font-medium text-muted-foreground">Тип узла</p>
{[["JH", "JumpHost", "var(--chart-1)"], ["EN", "Exit Node", "var(--chart-3)"], ["HR", "Home Router", "var(--chart-2)"]].map(([label, name, color]) => (
{[["JH", "JumpHost", "var(--chart-1)"], ["EN", "Exit Node", "var(--chart-3)"], ["HR", "Home Router", "var(--chart-2)"], ["IX", "IX Node", "var(--chart-4)"]].map(([label, name, color]) => (
<span key={label} className="flex items-center gap-2"><Badge variant="secondary" style={{ color }} className="min-w-8 justify-center text-[10px]">{label}</Badge><span>{name}</span></span>
))}
<span className="flex items-center gap-2"><span aria-hidden className="mx-1 size-3 rounded-full border border-current" />WAN · аплинк</span>
+1
View File
@@ -65,6 +65,7 @@ function typeBadgeVariant(
function typeBadgeLabel(type: ServerType): string {
if (type === "jump-host") return "JH"
if (type === "home-router") return "HR"
if (type === "ix-node") return "IX"
return "EN"
}
+25 -13
View File
@@ -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 }) => (
<div className="flex min-w-0 flex-col gap-1">
<span className="flex items-center gap-1.5 text-sm font-medium truncate">
{country ? <Flag code={row.original.id} /> : null}
{country && /^[a-z]{2}$/i.test(row.original.id) ? <Flag code={row.original.id} /> : null}
{row.original.label}
</span>
<div className="h-1 rounded-full bg-muted overflow-hidden">
@@ -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({
<p className="text-xs text-muted-foreground">Аналитика потребления</p>
{liveHint === "live" ? <Badge variant="success-light" size="sm">live</Badge> : null}
</div>
<p className="text-xs text-muted-foreground">
Разрезы рассчитаны по сохранённой выборке NetFlow. В рейтингах до 50 крупнейших строк, доли рассчитаны от всей выборки.
</p>
<Tabs value={slice} onValueChange={(v) => setSlice(String(v))} className="gap-3">
<TabsList variant="line" className="flex w-full shrink-0 flex-wrap gap-y-2 group-data-horizontal/tabs:h-auto [&_[data-slot=tabs-trigger]]:h-9 [&_[data-slot=tabs-trigger]]:flex-none">
<TabsTrigger value="applications">Приложения</TabsTrigger>
@@ -585,6 +595,7 @@ export function FlowAnalyticsDetail({
<TabsTrigger value="interfaces">Интерфейсы</TabsTrigger>
</TabsList>
<TabsContent value="applications">
<p className="mb-2 text-xs text-muted-foreground">Приложения определяются по протоколу и портам.</p>
<FlowBreakdownGrid rows={analytics?.applications ?? []} onPick={(row) => pickBreakdown("application", row)} />
</TabsContent>
<TabsContent value="categories">
@@ -602,8 +613,9 @@ export function FlowAnalyticsDetail({
<TabsContent value="map">
<FlowTrafficMap
edges={analytics?.mapEdges ?? []}
onSelectCountry={(iso) => {
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")
+9 -9
View File
@@ -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<string | null>(null)
@@ -77,7 +77,7 @@ function FlowTrafficMap({
{/^[a-z]{2}$/i.test(row.original.toCountry)
? <Flag code={row.original.toCountry} />
: null}
{row.original.toCountry}
{row.original.toCountry === "unknown" ? "Страна неизвестна" : row.original.toCountry}
{row.original.toAsn ? <span className="font-mono text-[10px] text-muted-foreground">AS{row.original.toAsn}</span> : null}
</span>
),
@@ -120,7 +120,7 @@ function FlowTrafficMap({
if (!edges.length) {
return (
<p className="text-sm text-muted-foreground py-8 text-center">
Страны подтянутся из кэша RIPEstat
Нет данных за период
</p>
)
}
@@ -150,10 +150,10 @@ function FlowTrafficMap({
className="cursor-pointer"
onMouseEnter={() => setHover(id)}
onMouseLeave={() => setHover(null)}
onClick={() => onSelectCountry?.(e.toCountry)}
onClick={() => onSelectEdge?.(e)}
>
<title>
{`${e.fromLabel} → ${e.toCountry} · ${e.category} · ${formatBytes(e.bytes)}`}
{`${e.fromLabel} → ${e.toCountry === "unknown" ? "Страна неизвестна" : e.toCountry} · ${e.category} · ${formatBytes(e.bytes)}`}
</title>
</path>
)
@@ -170,7 +170,7 @@ function FlowTrafficMap({
<g key={`d-${n.country}`}>
<circle cx={n.x} cy={n.y} r="7" className="fill-chart-2" />
<text x={n.x + 14} y={n.y + 4} className="fill-foreground text-[11px]">
{n.country}
{n.country === "unknown" ? "Без гео" : n.country}
</text>
</g>
))}
@@ -185,8 +185,8 @@ function FlowTrafficMap({
<DataGridShell
table={table}
recordCount={edges.length}
emptyMessage="Нет рёбер с известной страной"
onRowClick={(row) => onSelectCountry?.(row.toCountry)}
emptyMessage="Нет данных за период"
onRowClick={onSelectEdge}
/>
</div>
)
+93
View File
@@ -0,0 +1,93 @@
# Точки обмена трафиком
В **Управление → Точки обмена трафиком** оператор создаёт сегмент на сервере типа **IX Node**, сохраняет конфигурацию и подготавливает план через существующее подключение MCP. План отображается в **Планы и операции** и проходит штатное подтверждение. Сохранение черновика не выполняет записи на маршрутизаторах.
## Конфигурация сегмента
- **Площадка** указывает ID выделенной CHR, её локальный ASN и Router ID. Для одного сегмента можно указать до двух площадок.
- **Интерфейс** связывает площадку с WireGuard, GRE/IPsec или выделенным uplink. Для подготовленного вручную интерфейса задайте комментарий `IX:<имя сегмента>`. Интерфейс не должен принадлежать другой VRF или обслуживать статический маршрут в `main`.
- **Группа шлюзов** содержит служебный адрес, основной путь с интерфейсом, непосредственно достижимым шлюзом и контрольным адресом. У резерва другой интерфейс и своя проба. `onFailure` выбирает `block` или `segment-exit`.
- **BGP-сосед** привязан к интерфейсу площадки. Для публичного фида правила сопоставляют community с группой. Для частного соседа задаются группа и диапазоны, принадлежащие этому подключению. По умолчанию импорт и экспорт отклоняются, если правило не совпало.
- **Выходы** содержат ссылки на группы в порядке приоритета. NAT можно включить только на выделенном локальном IPv4-uplink.
Точки и адреса ниже учебные. Пример подготавливается на одной CHR, с отдельными уже настроенными транспортами и локальным uplink.
```json
{
"name": "compute",
"sites": [{
"serverId": 17,
"autonomousSystem": 64512,
"routerId": "192.0.2.17",
"interfaces": [
{"name": "wg-eu", "kind": "wireguard"},
{"name": "wg-arn", "kind": "wireguard"},
{"name": "wg-feed", "kind": "wireguard"},
{"name": "ix-wan", "kind": "uplink", "natIpv4": true}
],
"groups": [
{
"id": "europe",
"gateway": "198.18.0.1",
"primary": {"interface": "wg-eu", "nextHop": "10.0.1.2", "probe": "192.0.2.101"},
"backup": {"interface": "wg-arn", "nextHop": "10.0.2.2", "probe": "192.0.2.102"},
"onFailure": "block"
},
{
"id": "local",
"gateway": "198.18.0.2",
"primary": {"interface": "ix-wan", "nextHop": "10.0.3.2", "probe": "192.0.2.103"}
}
],
"exits": ["local"],
"neighbors": [{
"id": "feed",
"type": "public-feed",
"address": "10.0.4.2",
"localAddress": "10.0.4.1",
"remoteAs": 64513,
"binding": "wg-feed",
"rules": [{"community": "65000:120", "group": "europe"}]
}]
}]
}
```
Для создания WireGuard самим планом используйте `provisionWireguard` с `listenPort`, `localAddress`, `peerAddress` и `peerPublicKey`. Если задан `hubEndpoint`, после применения вкладка маршрутов выдаёт шаблон Linux с публичным ключом CHR. Шаблон использует `Table = off`. Оператор устанавливает маршруты и политику выбора источника на Linux отдельно. Для GRE/IPsec перед привязкой туннеля настройте IKEv2 и политику transport mode на обоих концах и отключите FastPath на интерфейсах GRE. В конфигурации IX укажите фиксированные `localEndpoint` и `endpoint`.
## MCP и ввод в эксплуатацию
`mm_ix_segments_list` и `mm_ix_segment_get` читают конфигурацию и наблюдения в разрешённой области. `mm_ix_change_plan` готовит изменение, `mm_ix_restore_plan` готовит восстановление сохранённой ревизии. Применение, сверка и журнал общие с остальными функциями MCP.
Перед запуском backend оператор применяет миграцию `0058_internet_exchange.sql` штатной подготовкой базы. Для подписанного PostgreSQL-профиля используется **source59**. Он добавляет в каталог три таблицы IX, сохраняет профиль source58 как точного предшественника и связывает новый профиль с хешем миграций.
При переходе source58 → source59 сначала сохраните подтверждённый текущий head. После миграции примените выдачу прав и защит из `ixWriterProfileEnrollmentSql(runtimeRole, captureRole)` в отдельной предварительно допущенной транзакции оператора. `source-catalogue-inspect` проверяет фактический каталог source59 на том же источнике и наборе ролей. Штатный `upgrade` принимает независимое подтверждение `application-writer-upgrade58-to59-approval-v1`, привязанное к прежнему head и новому каталогу. Runtime допускает только профиль, соответствующий миграциям текущего выпуска. Команда `node --import tsx backend/scripts/acceptance/ix-writer-profile.mts` проверяет права, блокировку записи и каталог на своей временной базе PostgreSQL.
Перед включением пользовательского трафика проверьте на выделенных CHR изоляцию, работу BGP, блокировку открытого GRE, обратные маршруты и переключение при отказе всех путей. Статус «Применена» означает, что конфигурация перечитана с RouterOS. Он не подтверждает прохождение пользовательского трафика.
## BGP и резервирование на RouterOS 7.24.5
Входной фильтр выбирает группу шлюзов по BGP community. RouterOS разрешает адрес, заданный фильтром `set gw`, через таблицу `main`. Поэтому IX создаёт там один служебный маршрут на группу BGP. Он направляет разрешение обратно в VRF IX, где отдельные ICMP-пробы выбирают основной, резервный путь или отбрасывание. Полученные префиксы и маршруты пользователей остаются в VRF IX.
Для служебных адресов групп BGP используйте уникальные для каждой CHR адреса из `198.18.0.0/15` или `fd00:198:18::/48`. При наличии другого маршрута с тем же служебным адресом в `main` MikrotikManager отклоняет применение.
Сценарий `node --import tsx backend/scripts/acceptance/ix-two-chr.mts --owned-test-pair` собирает конфигурацию тем же компилятором и применяет её через SSH на двух выделенных тестовых CHR. Он проверяет принятие префикса и передачу ICMP с одного источника на обоих путях, блокирует UDP основного WireGuard-туннеля на второй CHR, проверяет переход и возврат при сохранённой BGP-сессии и удаляет временную конфигурацию. Время в отчёте измеряется от подтверждения удалённого изменения до наблюдения маршрута и включает задержки SSH.
Результат прогона 22 ноября 2026 года на двух CHR с RouterOS 7.24.5 stable:
| Проверка | Результат |
| --- | --- |
| eBGP и изоляция | Community `65000:120` направила `192.0.2.200/32` в заданную группу. Префикс присутствовал в VRF IX и отсутствовал в `main`. |
| Основной путь → резерв | `immediate-gw` перешёл на второй WireGuard через 28,842 с. На обоих путях получены 3 из 3 ICMP-ответов с одного источника. |
| Резерв → основной путь | Возврат наблюдался через 2,346 с. Получены 3 из 3 ICMP-ответов. Время запуска BGP-сессии осталось прежним. |
| Завершение | Проверено удаление объектов тестового сценария на обеих CHR. |
Повторный прогон с параметром `--ipv6` на той же паре CHR 23 ноября 2026 года подтвердил приём `2001:db8:3::200/128` по eBGP, его изоляцию от `main` и по 3 из 3 ICMPv6-ответов на основном пути, на резерве и после возврата. Переход наблюдался через 23,895 с, возврат — через 10,418 с. BGP-сессия сохранилась, временные объекты на обеих CHR удалены.
## Применение через REST и MCP
Сценарий `node --import tsx backend/scripts/acceptance/ix-gre-mcp.mts --owned-test-pair` запускает отдельный MikrotikManager с временной PostgreSQL. HTTP API сохраняет черновик. MCP создаёт план, отклоняет его исполнение до явного подтверждения и после подтверждения применяет конфигурацию через RouterOS REST по HTTPS с проверкой сертификатов. Проверяются терминальный статус `applied / passed`, перечитывание обеих площадок и идемпотентный повтор `mm_change_apply`.
Лимит изменений MCP должен покрывать все объекты обеих площадок и запись ревизии. Штатные 10 изменений в час не покрыли двухплощадочный стендовый план. В тесте лимит 40 временно задан только проверочному подключению, подтверждение плана остаётся обязательным.
На двух CHR с RouterOS 7.24.5 stable 23 ноября 2026 года тот же сценарий подтвердил GRE поверх IKEv2/IPsec в режиме transport. На каждой CHR наблюдались две активные IPsec SA и 3 из 3 ответов на ICMP через VRF IX. При отключённых политиках IPsec обе стороны получили 0 из 3 ответов. После повторного включения IPsec трафик восстановился. С обеих CHR удалены временные правила, туннели, учётные записи и сертификаты, служба HTTPS возвращена в исходное состояние.
+3 -1
View File
@@ -123,7 +123,9 @@
В «Подключения» → «Выдать доступ» администратор MCP может выбрать «Сервисное — бессрочный автономный доступ». Выберите нужные серверы и операции. Доступ действует до отзыва и переживает истечение обычной сессии портала. По умолчанию опасные действия требуют ручного подтверждения.
На шаге 3 можно включить «Выполнять планы без ручного подтверждения». Режим действует на все выданные ключу операции. Списки серверов, права, лимиты и правила защиты объектов продолжают действовать.
На шаге 3 можно включить «Выполнять планы без ручного подтверждения». Режим действует на все выданные ключу операции. Списки серверов, права и правила защиты объектов продолжают действовать.
Опция «Отключить квоты частоты для этого ключа» доступна при создании и редактировании сервисного подключения. Она снимает квоты на HTTP-запросы MCP, вызовы инструментов, одновременные чтения и изменения в час. В API ей соответствует `unlimitedRates: true`. Ключ менять не нужно. Ограничения размера плана и общей нагрузки сервера продолжают действовать.
Если план содержит `announcement`, клиент сначала публикует отдельное сообщение в чате с ID плана, целями, изменениями и предупреждениями из `changes`. Затем вызывает `mm_change_apply` с `planId`, `idempotencyKey` и `announcedPlanDigest`, равным `announcement.digest`. Manager проверяет контрольную сумму сохранённого плана. Публикацию сообщения обеспечивает клиент. В аудите такой запуск отмечается как допуск по политике подключения.
+1
View File
@@ -34,6 +34,7 @@ export const SERVER_FILTER_FIELDS: FilterField[] = [
{ value: "jump-host", label: "JumpHost" },
{ value: "exit-node", label: "Exit Node" },
{ value: "home-router", label: "Home Router" },
{ value: "ix-node", label: "IX Node" },
],
},
]
+3 -2
View File
@@ -1,9 +1,10 @@
import type { FilterQuery } from "@/components/reui/filters/filters-types"
import { SERVER_FILTER_FIELDS } from "./server-filter-fields"
import { serverTypeSchema } from "@mmapp/contracts/servers"
export type ServerTypeFilter = "all" | "jump-host" | "exit-node" | "home-router"
export type ServerTypeFilter = "all" | import("@/lib/data").ServerType
export const emptyServerQuery = (): FilterQuery => ({ id: "root", type: "group", combinator: "and", rules: [] })
const types = ["jump-host", "exit-node", "home-router"]
const types: readonly string[] = serverTypeSchema.options
const MAX_BYTES = 8192
const options = new Map(SERVER_FILTER_FIELDS.map(field => [field.id, new Set((field.options ?? []).map(option => option.value))]))
const record = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null && !Array.isArray(value)
+2 -2
View File
@@ -36,10 +36,10 @@ export function patchUptimeUrl(params: URLSearchParams, changes: Record<string,
export type UrlSetter<T> = Dispatch<SetStateAction<T>>
export type ResourceTypeFilter = "all" | "jump-host" | "exit-node" | "home-router"
export type ResourceTypeFilter = "all" | import("@mmapp/contracts/servers").ServerType
export function readResourceUrl(params: URLSearchParams) {
const raw = params.get("rtype")
const valid = raw === null || ["jump-host", "exit-node", "home-router"].includes(raw)
const valid = raw === null || ["jump-host", "exit-node", "home-router", "ix-node"].includes(raw)
const error = !valid || ["rq", "rtype"].some(key => params.getAll(key).length > 1)
? "В ссылке указаны неверные или повторяющиеся фильтры ресурсов." : null
return { search: params.get("rq") ?? "", type: (valid && raw !== null ? raw : "all") as ResourceTypeFilter, error }
+1 -1
View File
@@ -1,5 +1,5 @@
export type ServerStatus = "online" | "offline" | "degraded" | "unknown"
export type ServerType = "jump-host" | "exit-node" | "home-router"
export type ServerType = import("@mmapp/contracts/servers").ServerType
export interface WanUplink {
id: string
+2 -1
View File
@@ -1,3 +1,4 @@
import { serverTypeSchema } from "@mmapp/contracts/servers"
import { z } from "zod"
const context = { id: z.string().min(1), rosId: z.string().min(1), serverId: z.string().min(1), serverName: z.string(), family: z.enum(["ip", "ip6"]) }
@@ -19,7 +20,7 @@ export const firewallResponseSchema = z.object({
export const firewallServersSchema = z.array(z.object({
id: z.number().int().positive(), name: z.string(), host: z.string(), enabled: z.boolean(), country: z.string(),
type: z.enum(["jump-host", "exit-node", "home-router"]), site: z.string().default(""), asn: z.string().default(""),
type: serverTypeSchema, site: z.string().default(""), asn: z.string().default(""),
status: z.enum(["online", "offline", "degraded"]).nullable().default(null), latency: z.number().finite().nullable().default(null),
})).refine(rows => new Set(rows.map(row => row.id)).size === rows.length)
Loaded 100 of 115 files, more files were not shown because too many files have changed in this diff. Show more