Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d255694cda | ||
|
|
e85e347cbe | ||
|
|
165f5e1260 | ||
|
|
1af3755a77 | ||
|
|
42efc3191b | ||
|
|
c20b6b33c3 | ||
|
|
79ab7b45aa | ||
|
|
26249e1a4d | ||
|
|
136474ba95 | ||
|
|
f0e08f9731 | ||
|
|
87461daa42 | ||
|
|
e6b2108f79 | ||
|
|
bec8de7c95 | ||
|
|
e9639313c6 | ||
|
|
04c3b1e482 | ||
|
|
776dea6a77 | ||
|
|
f27fd035bc |
No files matched your search
@@ -19,12 +19,39 @@ jobs:
|
||||
tag: ${{ steps.release.outputs.tag }}
|
||||
bump: ${{ steps.release.outputs.bump }}
|
||||
release_url: ${{ steps.release.outputs.release_url }}
|
||||
backend_reuse_version: ${{ steps.changes.outputs.backend_reuse_version }}
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Check if the released backend can be reused
|
||||
id: changes
|
||||
shell: bash
|
||||
env:
|
||||
EVENT_NAME: ${{ gitea.event_name }}
|
||||
BEFORE_SHA: ${{ gitea.event.before }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if [[ "$EVENT_NAME" != push || ! "$BEFORE_SHA" =~ ^[0-9a-f]{40}$ ]] ||
|
||||
! git merge-base --is-ancestor "$BEFORE_SHA" HEAD 2>/dev/null; then
|
||||
exit 0
|
||||
fi
|
||||
# Compare with a published release, including changes from any failed earlier run.
|
||||
BASE_TAG=$(git describe --tags --abbrev=0 --match 'v[0-9]*.[0-9]*.[0-9]*' "$BEFORE_SHA" 2>/dev/null || true)
|
||||
if [[ ! "$BASE_TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || git diff --quiet "$BASE_TAG" HEAD --; then
|
||||
exit 0
|
||||
fi
|
||||
if git diff --quiet --no-renames "$BASE_TAG" HEAD -- . \
|
||||
':(exclude)app/**' ':(exclude)components/**' ':(exclude)lib/**' ':(exclude)shared/**' \
|
||||
':(exclude)entities/**' ':(exclude)public/**' ':(exclude)hooks/**' ':(exclude)tests/e2e/**' \
|
||||
':(exclude)Dockerfile.frontend' ':(exclude)next.config.ts' ':(exclude)tsconfig.json' \
|
||||
':(exclude)postcss.config.mjs' ':(exclude)eslint.config.mjs' ':(exclude)components.json' \
|
||||
':(exclude,top,glob)playwright*.config.ts'; then
|
||||
echo "backend_reuse_version=${BASE_TAG#v}" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
|
||||
- name: Test release version calculation
|
||||
run: node --test .ci/scripts/compute-release.test.mjs
|
||||
|
||||
@@ -42,17 +69,21 @@ jobs:
|
||||
test -f .ci/release/release-manifest.json
|
||||
|
||||
backend-test:
|
||||
needs: prepare-release
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
if: needs.prepare-release.outputs.backend_reuse_version == ''
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node 22
|
||||
if: needs.prepare-release.outputs.backend_reuse_version == ''
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
|
||||
- name: Start PostgreSQL 18
|
||||
if: needs.prepare-release.outputs.backend_reuse_version == ''
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
@@ -93,6 +124,7 @@ jobs:
|
||||
exit 1
|
||||
|
||||
- name: Install and test backend
|
||||
if: needs.prepare-release.outputs.backend_reuse_version == ''
|
||||
shell: bash
|
||||
env:
|
||||
REQUIRE_TEST_POSTGRES: "1"
|
||||
@@ -112,7 +144,7 @@ jobs:
|
||||
npm run test --prefix backend
|
||||
|
||||
- name: Stop PostgreSQL
|
||||
if: always()
|
||||
if: always() && needs.prepare-release.outputs.backend_reuse_version == ''
|
||||
shell: bash
|
||||
run: docker rm -f "${PG_CONTAINER:-}" 2>/dev/null || true
|
||||
|
||||
@@ -123,6 +155,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
if: needs.prepare-release.outputs.backend_reuse_version == ''
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure backend image name
|
||||
@@ -135,6 +168,7 @@ jobs:
|
||||
echo "IMAGE_NAME=${{ env.REGISTRY }}/${owner}/${stem}-backend" >> "${GITHUB_ENV}"
|
||||
|
||||
- name: Prepare backend build context
|
||||
if: needs.prepare-release.outputs.backend_reuse_version == ''
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
@@ -174,6 +208,7 @@ jobs:
|
||||
password: ${{ secrets.ACTIONS_PAT }}
|
||||
|
||||
- name: Build and push backend image
|
||||
if: needs.prepare-release.outputs.backend_reuse_version == ''
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .ci/docker/backend
|
||||
@@ -195,6 +230,18 @@ jobs:
|
||||
org.opencontainers.image.revision=${{ gitea.sha }}
|
||||
org.opencontainers.image.created=${{ gitea.event.head_commit.timestamp }}
|
||||
|
||||
- name: Tag the existing backend image for this release
|
||||
if: needs.prepare-release.outputs.backend_reuse_version != ''
|
||||
shell: bash
|
||||
env:
|
||||
BACKEND_VERSION: ${{ needs.prepare-release.outputs.backend_reuse_version }}
|
||||
RELEASE_VERSION: ${{ needs.prepare-release.outputs.version }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
docker buildx imagetools create --prefer-index=false \
|
||||
--tag "$IMAGE_NAME:$GITHUB_SHA" --tag "$IMAGE_NAME:$RELEASE_VERSION" \
|
||||
"$IMAGE_NAME:$BACKEND_VERSION"
|
||||
|
||||
frontend-image:
|
||||
needs: prepare-release
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@@ -617,7 +617,7 @@ function NetflowRateBadge({
|
||||
onClick={(e) => { e.stopPropagation(); onOpen?.(e) }}
|
||||
>
|
||||
<title>
|
||||
Поток NetFlow между узлами (как в «Трафик»: 5 мин, без overlay/mesh). Скорость канала — отдельно.
|
||||
Поток NetFlow между узлами за интервал карты, без overlay/mesh.
|
||||
</title>
|
||||
<rect className="network-map-focus-ring" x={-bw / 2 - 4} y={-bh / 2 - 4} width={bw + 8} height={bh + 8} rx="9" fill="none" stroke="var(--ring)" strokeWidth="2" />
|
||||
<rect
|
||||
@@ -1442,7 +1442,7 @@ function NetworkMapContent() {
|
||||
if (cancelled||request!==epoch) return
|
||||
const name = err instanceof Error ? err.name : ""
|
||||
if (name === "AbortError") return
|
||||
setFlowError(err instanceof Error?(err.name==="ZodError"?"Некорректный ответ: требуется полный снимок за 5 минут":err.message):"Ошибка NetFlow")
|
||||
setFlowError(err instanceof Error?(err.name==="ZodError"?"Ответ содержит неполные или противоречивые данные снимка":err.message):"Ошибка NetFlow")
|
||||
}).finally(()=>{inFlight=false;if(!cancelled&&request===epoch)setFlowLoading(false)})
|
||||
}
|
||||
queueMicrotask(()=>{if(!cancelled)tick()})
|
||||
@@ -2224,7 +2224,7 @@ function NetworkMapContent() {
|
||||
<span className="text-[10px] border border-border rounded px-1.5 py-0.5">
|
||||
{useLiveData ? (readAt ? "Снимок API · ответ "+new Date(readAt).toLocaleTimeString("ru-RU")+" (время браузера)" : "Данные карты неизвестны") : "Демо · mock"}
|
||||
</span>
|
||||
{useLiveData&&<span>{showNetflow||showServices?(flowReadAt?"NetFlow · 5 мин · ответ "+new Date(flowReadAt).toLocaleTimeString("ru-RU")+" (время браузера) · "+(flowListener?"приёмник включён":"приёмник выключен"):"Данные NetFlow неизвестны"):"NetFlow: отображение и обновление выключены"}</span>}
|
||||
{useLiveData&&<span>{showNetflow||showServices?(flowReadAt?`NetFlow · ${mapWindowSec / 60} мин${mapWindowSec < 300 ? " из 5 запрошенных" : ""} · ответ `+new Date(flowReadAt).toLocaleTimeString("ru-RU")+" (время браузера) · "+(flowListener?"приёмник включён":"приёмник выключен"):"Данные NetFlow неизвестны"):"NetFlow: отображение и обновление выключены"}</span>}
|
||||
{useLiveData&&flowLoading&&(showNetflow||showServices)&&<span role="status">Обновление NetFlow…</span>}
|
||||
{useLiveData&&flowError&&(showNetflow||showServices)&&<span role="alert" className="text-destructive [overflow-wrap:anywhere]">NetFlow: {flowError}. {flowReadAt?"Сохранён предыдущий снимок NetFlow; актуальность не подтверждена.":"Первоначальная загрузка NetFlow не подтверждена."}</span>}
|
||||
{useLiveData&&dnsError&&<span role="alert" className="text-destructive [overflow-wrap:anywhere]">DNS: {dnsError}. Связи по неизвестному адресу не подтверждены.</span>}
|
||||
@@ -2934,7 +2934,7 @@ function NetworkMapContent() {
|
||||
? "Ping и/или TX/RX — с последнего прогона speed-пробы; проба сопоставляется с этим GRE по WAN и интерфейсам. "
|
||||
: useLiveData?"Измерения GRE неизвестны: нет подходящей пробы. ":"Измерения демонстрационные. "}
|
||||
{spGr?"Проба "+spGr.id+" · время Ping: "+(spGr.lastPingAt?new Date(spGr.lastPingAt).toLocaleString("ru-RU"):"неизвестно")+" · время TX/RX неизвестно. ":""}
|
||||
Поток — снимок NetFlow за 5 мин (как в «Трафик»: без overlay/mesh), не ёмкость канала.
|
||||
Поток по данным NetFlow за {mapWindowSec / 60} мин, без overlay/mesh.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+66
-32
@@ -2,7 +2,7 @@
|
||||
|
||||
import { useState, useMemo, useEffect, type ReactNode } from "react"
|
||||
import { Alert, AlertTitle, AlertDescription } from "@/components/reui/alert"
|
||||
import { projectTrafficObservations, type TrafficObservationMeta } from "@/lib/traffic-observations"
|
||||
import { peakOfCombinedTraffic, projectTrafficObservations, type TrafficObservationMeta } from "@/lib/traffic-observations"
|
||||
import { projectTrafficBindingStatus } from "@/lib/traffic-binding-status"
|
||||
import { TrafficObservationStatus } from "@/components/traffic/traffic-observation-status"
|
||||
import { TrafficDataTime } from "@/components/traffic/traffic-data-time"
|
||||
@@ -25,7 +25,7 @@ import { useDataSource } from "@/lib/data-source"
|
||||
import { useTrafficLive } from "@/hooks/use-traffic-live"
|
||||
import { useFlowLive } from "@/hooks/use-flow-live"
|
||||
import { requestJson } from "@/shared/api/http-client"
|
||||
import { getFlowAnalytics, getFlowClients, getFlowExporters, getFlowMonthly, getTrafficFlows } from "@/shared/api/traffic-flow"
|
||||
import { getFlowAnalytics, getFlowClients, getFlowExporters, getFlowMonthly, getTrafficFlowSettings, getTrafficFlows } from "@/shared/api/traffic-flow"
|
||||
import { listServers } from "@/shared/api/servers"
|
||||
import { FlowOverlaySheet } from "@/components/traffic/flow-overlay-sheet"
|
||||
import { FlowAnalyticsDetail, FlowEntityCardView } from "@/components/traffic/flow-analytics-panel"
|
||||
@@ -56,32 +56,31 @@ function addSeries(a: number[], b: number[]): number[] {
|
||||
return a.map((v, i) => v + (b[i] ?? 0))
|
||||
}
|
||||
|
||||
function flowIngestLine(stats: FlowStatsDto | null): string | null {
|
||||
type FlowCollectorStatus = Pick<FlowStatsDto, "listenerBound" | "listenerAddress" | "packetsReceived" | "lastDatagramAt" | "lastExporterIp" | "lastError" | "collectorCountersSource">
|
||||
|
||||
function flowIngestLine(stats: FlowCollectorStatus | null): string | null {
|
||||
if (!stats) return null
|
||||
const listener = stats.listenerBound
|
||||
? (stats.listenerAddress ?? "слушает")
|
||||
: "не слушает"
|
||||
: "приём не подтверждён"
|
||||
const last = stats.lastDatagramAt
|
||||
? new Date(stats.lastDatagramAt).toLocaleString("ru-RU")
|
||||
: "—"
|
||||
const exporter = stats.lastExporterIp ? ` · ${stats.lastExporterIp}` : ""
|
||||
const err = stats.lastError ? ` · ${stats.lastError}` : ""
|
||||
return `Коллектор: ${listener} · пакеты ${stats.packetsReceived ?? 0} · последний ${last}${exporter}${err}`
|
||||
const hint = stats.collectorCountersSource === "last-known" ? " · последние известные данные" : ""
|
||||
return `Коллектор: ${listener} · пакеты ${stats.packetsReceived ?? "—"} · последний ${last}${exporter}${hint}${err}`
|
||||
}
|
||||
|
||||
function flowEmptyHint(stats: FlowStatsDto | null, collectorAlive?: boolean): string | undefined {
|
||||
function flowEmptyHint(stats: FlowCollectorStatus | null): string | undefined {
|
||||
if (!stats) return undefined
|
||||
if (stats.lastError) return stats.lastError
|
||||
if (stats.collectorCountersSource === "last-known") return "Текущий приём IPFIX не подтверждён."
|
||||
if (stats.listenerBound === false) return "Коллектор UDP не слушает."
|
||||
if (stats.packetsReceived) {
|
||||
return `IPFIX приходит (${stats.lastExporterIp ?? "экспортёр"}), но сессии ещё не записаны.`
|
||||
return "В выбранном интервале нет сохранённых потоков."
|
||||
}
|
||||
if (stats.listenerBound === false && !collectorAlive) {
|
||||
return "Коллектор UDP не слушает. Подключите JH ещё раз — ingest включится автоматически."
|
||||
}
|
||||
if (stats.listenerBound || collectorAlive) {
|
||||
return "Коллектор жив, IPFIX ещё не доходит. На jump-host у target Src должен быть 0.0.0.0 (авто)."
|
||||
}
|
||||
return "IPFIX ещё не доходит до коллектора. На jump-host у target Src должен быть 0.0.0.0 (авто). На хосте MM проверьте bind 10.255.254.1:4739 после wg-flow."
|
||||
return stats.listenerBound ? "Коллектор слушает, датаграммы IPFIX ещё не получены." : undefined
|
||||
}
|
||||
|
||||
// ─── data model ───────────────────────────────────────────────────────────────
|
||||
@@ -803,7 +802,7 @@ export default function TrafficPage() {
|
||||
const [trafficRetry, setTrafficRetry] = useState(0)
|
||||
const [liveRetry, setLiveRetry] = useState(0)
|
||||
const trafficKey = JSON.stringify([isLive, backendUrl, range, groupMode, trafficRetry])
|
||||
const [trafficState, setTrafficState] = useState<{ key: string; status: "ready" | "error"; receivedAt?: number; error?: string } | null>(null)
|
||||
const [trafficState, setTrafficState] = useState<{ key: string; status: "ready" | "error"; receivedAt?: number; peaks?: { rx: number | null; tx: number | null }; error?: string } | null>(null)
|
||||
const trafficReady = trafficState?.key === trafficKey && trafficState.status === "ready"
|
||||
const trafficLoading = trafficState?.key !== trafficKey
|
||||
const trafficError = trafficState?.key === trafficKey ? trafficState.error : undefined
|
||||
@@ -823,11 +822,17 @@ export default function TrafficPage() {
|
||||
const [flowScope, setFlowScope] = useState<FlowScope>("servers")
|
||||
const [flowRetry, setFlowRetry] = useState(0)
|
||||
const flowKey = JSON.stringify([isLive, groupMode, backendUrl, range, flowScope, flowRetry])
|
||||
const [flowResult, setFlowResult] = useState<{ key: string; receivedAt?: number; monthlyServers?: ServerRead[]; stats?: FlowStatsDto; exporters?: FlowEntityCard[]; clients?: FlowEntityCard[]; error?: string } | null>(null)
|
||||
const [flowResult, setFlowResult] = useState<{ key: string; receivedAt?: number; monthlyServers?: ServerRead[]; stats?: FlowStatsDto; exporters?: FlowEntityCard[]; clients?: FlowEntityCard[]; cardWindow?: FlowStatsDto["sourceWindow"]; error?: string } | null>(null)
|
||||
const currentFlows = flowResult?.key === flowKey ? flowResult : null
|
||||
const liveBusy = isLive && groupMode === "flows" && !currentFlows
|
||||
const liveError = currentFlows?.error
|
||||
const flowStats = currentFlows?.stats ?? null
|
||||
const [flowCollector, setFlowCollector] = useState<{ url: string; status: FlowCollectorStatus | null; receivedAt?: number } | null>(null)
|
||||
const collectorReading = flowCollector?.url === backendUrl ? flowCollector : { status: flowStats, receivedAt: currentFlows?.receivedAt }
|
||||
const collectorStatus: FlowCollectorStatus | null = collectorReading.status && collectorReading.receivedAt !== undefined
|
||||
&& trafficClock - collectorReading.receivedAt > 35_000
|
||||
? { ...collectorReading.status, listenerBound: false, collectorCountersSource: "last-known" }
|
||||
: collectorReading.status
|
||||
const flowExporters = useMemo(() => currentFlows?.exporters ?? [], [currentFlows])
|
||||
const flowClients = useMemo(() => currentFlows?.clients ?? [], [currentFlows])
|
||||
const [flowMonth, setFlowMonth] = useState(() => new Date().toISOString().slice(0, 7))
|
||||
@@ -882,7 +887,7 @@ export default function TrafficPage() {
|
||||
try {
|
||||
const q = encodeURIComponent(targetRange === "30d" ? "24h" : targetRange)
|
||||
const [srvRes, usersRes, ifacesRes] = await Promise.all([
|
||||
apiFetch<{ servers: LiveTrafficServer[] }>(`/api/traffic/servers?range=${q}`),
|
||||
apiFetch<{ servers: LiveTrafficServer[]; peaks?: { rx: number | null; tx: number | null } }>(`/api/traffic/servers?range=${q}`),
|
||||
apiFetch<{ users: UserTraffic[] }>(`/api/traffic/users?range=${q}`),
|
||||
apiFetch<{ interfaces: BoundIfaceTraffic[] }>(`/api/traffic/bound-interfaces?range=${q}`),
|
||||
])
|
||||
@@ -897,7 +902,7 @@ export default function TrafficPage() {
|
||||
setLiveServerOwner(JSON.stringify([isLive, backendUrl]))
|
||||
setLiveUsers(mappedUsers)
|
||||
setLiveBoundIfaces(liveIfaces)
|
||||
setTrafficState({ key, status: "ready", receivedAt: Date.now() })
|
||||
setTrafficState({ key, status: "ready", receivedAt: Date.now(), peaks: srvRes.peaks })
|
||||
setSelectedId((prev) => {
|
||||
if (groupMode === "users") return mappedUsers.some((x) => x.id === prev) ? prev : (mappedUsers[0]?.id ?? "")
|
||||
if (groupMode === "ifaces") return liveIfaces.some((x) => x.id === prev) ? prev : (liveIfaces[0]?.id ?? "")
|
||||
@@ -911,6 +916,28 @@ export default function TrafficPage() {
|
||||
return () => { cancelled = true }
|
||||
}, [apiFetch, isLive, groupMode, backendUrl, trafficRetry, range])
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLive || groupMode !== "flows" || range === "30d") return
|
||||
const controller = new AbortController()
|
||||
let timer: ReturnType<typeof setTimeout>
|
||||
const refresh = async () => {
|
||||
try {
|
||||
const status = await getTrafficFlowSettings(backendUrl, { signal: controller.signal })
|
||||
if (!controller.signal.aborted) setFlowCollector({ url: backendUrl, status, receivedAt: Date.now() })
|
||||
} catch {
|
||||
if (!controller.signal.aborted) setFlowCollector(current => ({
|
||||
url: backendUrl,
|
||||
status: current?.url === backendUrl && current.status
|
||||
? { ...current.status, listenerBound: false, collectorCountersSource: "last-known" }
|
||||
: null,
|
||||
}))
|
||||
}
|
||||
if (!controller.signal.aborted) timer = setTimeout(refresh, 15_000)
|
||||
}
|
||||
timer = setTimeout(refresh, 15_000)
|
||||
return () => { controller.abort(); clearTimeout(timer) }
|
||||
}, [isLive, groupMode, range, backendUrl])
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLive || effectiveMode !== "flows") return
|
||||
let cancelled = false
|
||||
@@ -932,7 +959,8 @@ export default function TrafficPage() {
|
||||
if (cancelled) return
|
||||
const clients = await getFlowClients(backendUrl, range)
|
||||
if (cancelled) return
|
||||
setFlowResult({ key: flowKey, receivedAt: Date.now(), stats, exporters: exporters.exporters, clients: clients.clients })
|
||||
setFlowResult({ key: flowKey, receivedAt: Date.now(), stats, exporters: exporters.exporters, clients: clients.clients,
|
||||
cardWindow: flowScope === "users" ? clients.sourceWindow : exporters.sourceWindow })
|
||||
setSelectedId(prev => {
|
||||
const list = flowScope === "users" ? clients.clients : exporters.exporters
|
||||
return list.some(x => x.id === prev) ? prev : (list[0]?.id ?? "")
|
||||
@@ -1115,14 +1143,14 @@ export default function TrafficPage() {
|
||||
: effectiveMode === "ifaces"
|
||||
? activeBoundIfaces
|
||||
: activeServerTraffic
|
||||
const totalRx = kpiSource.every(s => s.rxNow !== null) ? kpiSource.reduce((a, s) => a + (s.rxNow ?? 0), 0) : null
|
||||
const totalTx = kpiSource.every(s => s.txNow !== null) ? kpiSource.reduce((a, s) => a + (s.txNow ?? 0), 0) : null
|
||||
const peakRx = kpiSource.every(s => s.rxPeak !== null) ? kpiSource.reduce((a, s) => Math.max(a, s.rxPeak ?? 0), 0) : null
|
||||
const peakTx = kpiSource.every(s => s.txPeak !== null) ? kpiSource.reduce((a, s) => Math.max(a, s.txPeak ?? 0), 0) : null
|
||||
const totalRx = kpiSource.length && kpiSource.every(s => s.rxNow !== null) ? kpiSource.reduce((a, s) => a + (s.rxNow ?? 0), 0) : null
|
||||
const totalTx = kpiSource.length && kpiSource.every(s => s.txNow !== null) ? kpiSource.reduce((a, s) => a + (s.txNow ?? 0), 0) : null
|
||||
const exactPeaks = effectiveMode === "servers" && trafficReady ? trafficState?.peaks : undefined
|
||||
const peakRx = exactPeaks ? exactPeaks.rx : peakOfCombinedTraffic(kpiSource.map(s => s.rxSeries))
|
||||
const peakTx = exactPeaks ? exactPeaks.tx : peakOfCombinedTraffic(kpiSource.map(s => s.txSeries))
|
||||
|
||||
const visibleSortFields = SORT_FIELDS.filter(s => !s.modesOnly || s.modesOnly.includes(effectiveMode))
|
||||
const ingestLine = flowIngestLine(flowStats)
|
||||
const collectorAlive = Boolean(flowStats?.listenerBound || flowStats?.packetsReceived)
|
||||
const ingestLine = flowIngestLine(collectorStatus)
|
||||
const flowError = liveError
|
||||
|| flowLiveError
|
||||
|| (displayedFlow?.degraded ? "Коллектор перегружен: упрощённая аналитика" : null)
|
||||
@@ -1138,7 +1166,7 @@ export default function TrafficPage() {
|
||||
},
|
||||
{
|
||||
id: "bps",
|
||||
label: "Скорость",
|
||||
label: "Скорость IPFIX",
|
||||
value: displayedFlow ? fmtRate(displayedFlow.bpsNow / 1_000_000) : "—",
|
||||
hint: flowLiveHint,
|
||||
icon: <ActivityIcon className="size-4" />,
|
||||
@@ -1163,29 +1191,33 @@ export default function TrafficPage() {
|
||||
const counterKpiItems = [
|
||||
{
|
||||
id: "rx",
|
||||
label: "RX",
|
||||
label: "Сумма RX",
|
||||
value: fmtRate(totalRx),
|
||||
hint: effectiveMode === "servers" ? "Интерфейсы и туннели" : undefined,
|
||||
icon: <ArrowDownIcon className="size-4" />,
|
||||
iconClassName: "text-success",
|
||||
},
|
||||
{
|
||||
id: "tx",
|
||||
label: "TX",
|
||||
label: "Сумма TX",
|
||||
value: fmtRate(totalTx),
|
||||
hint: effectiveMode === "servers" ? "Интерфейсы и туннели" : undefined,
|
||||
icon: <ArrowUpIcon className="size-4" />,
|
||||
iconClassName: "text-info",
|
||||
},
|
||||
{
|
||||
id: "peak-rx",
|
||||
label: "Пик RX",
|
||||
label: "Пик суммы RX",
|
||||
value: fmtRate(peakRx),
|
||||
hint: exactPeaks ? "По общим замерам" : "По интервалам графика",
|
||||
icon: <TrendingUpIcon className="size-4" />,
|
||||
iconClassName: "text-warning",
|
||||
},
|
||||
{
|
||||
id: "peak-tx",
|
||||
label: "Пик TX",
|
||||
label: "Пик суммы TX",
|
||||
value: fmtRate(peakTx),
|
||||
hint: exactPeaks ? "По общим замерам" : "По интервалам графика",
|
||||
icon: <TrendingUpIcon className="size-4" />,
|
||||
iconClassName: "text-warning",
|
||||
},
|
||||
@@ -1250,6 +1282,8 @@ export default function TrafficPage() {
|
||||
</Button>
|
||||
</div>
|
||||
{isLive && <TrafficDataTime label="Получение списка потоков" receivedAt={currentFlows?.receivedAt} source="API" />}
|
||||
{flowStats?.sourceWindow && <p role="status" className="text-sm text-muted-foreground">Сводка потоков за последние {flowStats.sourceWindow.effectiveMinutes} мин из запрошенных {flowStats.sourceWindow.requestedMinutes} мин.</p>}
|
||||
{currentFlows?.cardWindow && <p role="status" className="text-sm text-muted-foreground">Данные карточек за последние {currentFlows.cardWindow.effectiveMinutes} мин из запрошенных {currentFlows.cardWindow.requestedMinutes} мин.</p>}
|
||||
{liveBusy && <p role="status">Загрузка потоков…</p>}
|
||||
{liveError && <Alert variant="destructive" role="alert"><AlertTitle>Потоки недоступны</AlertTitle><AlertDescription>{liveError}<Button size="sm" variant="outline" onClick={() => setFlowRetry(value => value + 1)}>Повторить загрузку потоков</Button></AlertDescription></Alert>}
|
||||
{flowError && !liveError && (
|
||||
@@ -1327,7 +1361,7 @@ export default function TrafficPage() {
|
||||
))}
|
||||
{!liveBusy && !liveError && (range === "30d" ? visibleMonthlyServers.length : sortedFlowCards.length) === 0 ? (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{range === "30d" ? (q ? "Нет серверов по этому запросу" : "В каталоге нет серверов") : flowEmptyHint(flowStats, collectorAlive) ?? "Нет экспортёров IPFIX. Подключите jump-host."}
|
||||
{range === "30d" ? (q ? "Нет серверов по этому запросу" : "В каталоге нет серверов") : flowEmptyHint(collectorStatus) ?? "Нет экспортёров IPFIX."}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
@@ -1356,7 +1390,7 @@ export default function TrafficPage() {
|
||||
excludeOverlay={flowExcludeOverlay}
|
||||
onExcludeOverlay={setFlowExcludeOverlay}
|
||||
liveHint={flowLiveHint}
|
||||
emptyHint={analyticsLoading ? "Загрузка аналитики…" : currentAnalytics?.error ? "Не удалось получить данные выбранного среза" : flowEmptyHint(flowStats, collectorAlive)}
|
||||
emptyHint={analyticsLoading ? "Загрузка аналитики…" : currentAnalytics?.error ? "Не удалось получить данные выбранного среза" : flowEmptyHint(collectorStatus)}
|
||||
/>
|
||||
</FramePanel>
|
||||
</Frame>
|
||||
|
||||
@@ -555,6 +555,13 @@
|
||||
"operations": [],
|
||||
"reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет."
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/mcp/server-choices",
|
||||
"status": "excluded",
|
||||
"operations": [],
|
||||
"reason": "Каталог выбора серверов для выдачи доступа в панели MCP."
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/mcp/connections/:id/grants",
|
||||
|
||||
@@ -12,6 +12,7 @@ function evaluate(source,mocks={}){
|
||||
return module.exports
|
||||
}
|
||||
const contracts=evaluate(readFileSync(resolve(here,'../../packages/contracts/src/traffic-flow.ts'),'utf8'))
|
||||
const readWindow=evaluate(readFileSync(resolve(here,'../src/services/traffic-flow-read-window.ts'),'utf8'))
|
||||
let quality=null,slotReleases=0
|
||||
const qualityHelper=evaluate(readFileSync(resolve(here,'../src/modules/mcp/flow-read-quality.ts'),'utf8'),{
|
||||
'@mmapp/contracts/traffic-flow':contracts,'../../services/traffic-flow-ingest.js':{getFlowRuntimeCounters:async()=>({dataQuality:quality})},'./traffic-history-actions.js':{assertTrafficHistoryAccess:async()=>{}},
|
||||
@@ -21,6 +22,7 @@ const window={rows:[],topRowsPerBucketLimit:20,topConversationsLimit:20,since:'f
|
||||
class McpError extends Error{constructor(code,message,statusCode){super(message);this.code=code;this.statusCode=statusCode}}
|
||||
const mocks={
|
||||
'./flow-read-quality.js':qualityHelper,
|
||||
'../../services/traffic-flow-read-window.js':readWindow,
|
||||
'@mmapp/contracts/traffic-flow':{...contracts,flowAnalyticsDtoSchema:{parse:x=>x},flowMapHopsDtoSchema:{parse:x=>x}},
|
||||
'../../services/traffic-flow-analytics.js':{buildFlowAnalytics:async()=>dto,getFlowMonthly:async()=>({month:'2026-10',bytes:0,countries:[],services:[],asns:[]}),safeBuildLiveFlowSample:async()=>({event:'sample',data:dto}),LIVE_ANALYTICS_MINUTES:5},
|
||||
'../../services/traffic-flow-map-hops.js':{buildFlowMapHops:async()=>({live:false,hops:[]})},
|
||||
|
||||
@@ -48,6 +48,7 @@ const RULES: Rule[] = [
|
||||
{ methods: ["POST"], match: p => p === "/api/mcp/oauth/consent", permission: "mm:mcp:read" },
|
||||
{ methods: ["POST"], match: p => /^\/api\/mcp\/connections\/[^/]+\/revalidate$/.test(p), permission: "mm:mcp:read" },
|
||||
{ methods: ["GET"], match: p => p === "/api/mcp/catalog", permission: "mm:mcp:read" },
|
||||
{ methods: ["GET"], match: p => p === "/api/mcp/server-choices", permission: "mm:mcp:read" },
|
||||
{ methods: ["GET"], match: p => p.startsWith("/api/mcp/devices/"), permission: "mm:mcp:read" },
|
||||
{ methods: ["GET"], match: p => /^\/api\/mcp\/(?:overview|connections|requests|plans|operations|protections|drift|audit|artifacts|routing-profiles)(?:\/|$)/.test(p), permission: "mm:mcp:read" },
|
||||
{ methods: ["POST"], match: p => /^\/api\/mcp\/plans\/(?:[^/]+)\/decision$/.test(p), permission: "mm:mcp:approve" },
|
||||
|
||||
@@ -32,6 +32,7 @@ import { subscribeLogDevice } from "../../services/router-logs-collector.js"
|
||||
import { ospfFilterFields, rosResources, readRosSnapshot, resourceById, routerClient, validateRosStep, splitOspfUnsetUpdate } from "./resources.js"
|
||||
import { registerMcpOperation, listMcpOperations } from "./registry.js"
|
||||
import { accessGeneration, decryptSecret, encryptSecret, fingerprint, MAX_MCP_PUBLIC_ARRAY_ITEMS, MAX_MCP_PUBLIC_STRING_LENGTH, McpError, redact } from "./security.js"
|
||||
import { hasUnattendedPlanAuthority } from "./policy.js"
|
||||
import { effectiveMcpConnection } from "./store.js"
|
||||
import type { McpAction, ActionContext, ChangeEvidence } from "./action-types.js"
|
||||
import { registerRoutingActions } from "./routing-actions.js"
|
||||
@@ -623,11 +624,15 @@ add("changes.read", "mm_change_get", "Просмотр своего плана",
|
||||
const { loadPlan, publicPlan } = await import("./runtime.js")
|
||||
const plan = await loadPlan(String(input.planId))
|
||||
await assertPlanReadScope(plan, context)
|
||||
return publicPlan(plan)
|
||||
return publicPlan(plan, hasUnattendedPlanAuthority(context.connection.policy) && plan.policy_version === context.connection.policy.version)
|
||||
},
|
||||
})
|
||||
add("changes.apply", "mm_change_apply", "Применить свой план", "mcp", null, "write", "connection", z.object({ planId: z.string().uuid(), idempotencyKey: z.string().min(1).max(200) }).strict(), {
|
||||
read: async (input, context) => (await import("./runtime.js")).applyPlan(context.principal, String(input.planId), String(input.idempotencyKey)),
|
||||
add("changes.apply", "mm_change_apply", "Применить свой план", "mcp", null, "write", "connection", z.object({
|
||||
planId: z.string().uuid(),
|
||||
idempotencyKey: z.string().min(1).max(200),
|
||||
announcedPlanDigest: z.string().regex(/^[0-9a-f]{64}$/).optional().describe("Значение announcement.digest. Передавайте только после отдельного видимого сообщения с точным планом в чате."),
|
||||
}).strict(), {
|
||||
read: async (input, context) => (await import("./runtime.js")).applyPlan(context.principal, String(input.planId), String(input.idempotencyKey), input.announcedPlanDigest as string | undefined),
|
||||
})
|
||||
add("changes.cancel", "mm_change_cancel", "Отменить свой незапущенный план", "mcp", null, "read", "connection", z.object({ planId: z.string().uuid() }).strict(), {
|
||||
read: async (input, context) => {
|
||||
|
||||
@@ -22,8 +22,8 @@ import { getFlowMonthly } from "../../services/traffic-flow-analytics.js"
|
||||
import { readPublicTrafficSettings, updateTrafficSettings } from "../../services/traffic-collector.js"
|
||||
import { readUptimeSettings, updateUptimeSettings, readProbeRows, replaceProbes } from "../../services/uptime-collector.js"
|
||||
import { readSpeedProbeCatalog, replaceSpeedProbeCatalog } from "../../services/speed-probe-catalog.js"
|
||||
import { getTrafficFlowSettingsRow, toTrafficFlowSettingsDto } from "../../services/traffic-flow-settings.js"
|
||||
import { getFlowListenerState } from "../../services/traffic-flow-ingest.js"
|
||||
import { getTrafficFlowSettingsRow } from "../../services/traffic-flow-settings.js"
|
||||
import { readTrafficFlowSettingsStatus } from "../../services/traffic-flow-ingest.js"
|
||||
import { withFlowConfigurationLock, getFlowHostStatus, reconcileFlowHost } from "../../services/traffic-flow-host-agent.js"
|
||||
import { agentSocket } from "../../services/traffic-flow-agent-client.js"
|
||||
import { getStatistics, getStatisticsPivot, pivotDimsConflict } from "../../services/statistics-aggregate.js"
|
||||
@@ -185,7 +185,7 @@ export function registerApplicationActions(register: typeof add) {
|
||||
change("traffic.settings.update", "mm_traffic_settings_update_plan", "План настроек сбора трафика", "mm:traffic:write", z.object({ settings: trafficSettings }).strict(), readTrafficConfiguration, async input => { const result = await updateTrafficSettings(trafficSettings.parse(input.settings)); await refreshScheduler(); return result })
|
||||
query("uptime.settings.read", "mm_uptime_settings_get", "Настройки мониторинга и проб", "mm:uptime:read", async () => readUptimeSettings())
|
||||
change("uptime.settings.update", "mm_uptime_settings_update_plan", "План расписания мониторинга и проб", "mm:uptime:write", z.object({ settings: uptimeSettings }).strict(), readUptimeConfiguration, async input => { const result = await updateUptimeSettings(uptimeSettings.parse(input.settings)); await refreshScheduler(); return result })
|
||||
query("netflow.settings.read", "mm_netflow_settings_get", "Публичные настройки NetFlow", "mm:traffic:read", async () => toTrafficFlowSettingsDto(getFlowListenerState()))
|
||||
query("netflow.settings.read", "mm_netflow_settings_get", "Публичные настройки NetFlow", "mm:traffic:read", readTrafficFlowSettingsStatus)
|
||||
query("scheduler.runs", "mm_scheduler_runs_list", "История заданий", "mm:settings:admin", async input => listSchedulerRuns({ limit: Number(input.limit), offset: Number(input.offset), jobKey: input.jobKey as string | undefined }), z.object({ ...paging, jobKey: z.enum(JOB_KEYS).optional() }).strict())
|
||||
query("servers.api-ping.settings.read", "mm_servers_api_ping_settings_get", "Настройки API-ping серверов", "mm:servers:read", async () => getServersApiPingSettings())
|
||||
change("servers.api-ping.settings.update", "mm_servers_api_ping_settings_update_plan", "План API-ping серверов", "mm:servers:write", z.object({ settings: z.object({ enabled: z.boolean().optional(), intervalSec: z.number().int().min(10).max(86400).optional() }).strict().refine(value => Object.keys(value).length > 0) }).strict(), readServersApiPingConfiguration, async input => { const result = await updateServersApiPingSettings(input.settings as Parameters<typeof updateServersApiPingSettings>[0]); await refreshScheduler(); return result })
|
||||
|
||||
@@ -133,7 +133,9 @@ async function executeRestore(operationId: string, principal: McpPrincipal,origi
|
||||
if (ids.some(id => !connection.policy.scope.serverIds.includes(id) || !evidence.objects.some(object => object.kind === "server" && object.serverId === id))) throw new McpError("SERVER_DENIED", "Область восстановления изменилась")
|
||||
const { rows: grants } = await pool.query("SELECT payload FROM mcp_grants WHERE connection_id=$1 AND revoked_at IS NULL AND payload->>'credentialId'=$2", [connection.id, principal.credentialId])
|
||||
if (grants.some(grant => !grant.payload.scope?.allowGlobal || !grant.payload.scope?.operationIds?.includes("database.restore") || ids.some(id => !grant.payload.scope?.serverIds?.includes(id)))) throw new McpError("PERMISSION_DENIED", "Временный допуск не разрешает восстановление")
|
||||
if ((await pool.query("SELECT 1 FROM mcp_protections WHERE object_ref->>'protection'='deny' LIMIT 1")).rows.length) throw new McpError("PROTECTED_OBJECT", "Восстановление заменяет запрещённый защищённый объект")
|
||||
const { rows: [protection] } = await pool.query<{ denied: boolean }>("SELECT (object_ref->>'protection'='deny') AS denied FROM mcp_protections WHERE object_ref->>'protection'='deny' OR $1::boolean ORDER BY denied DESC LIMIT 1", [!!plan.evidence.approvalSource])
|
||||
if (protection?.denied) throw new McpError("PROTECTED_OBJECT", "Восстановление заменяет запрещённый защищённый объект")
|
||||
if (protection) throw new McpError("APPROVAL_REQUIRED", "Полное восстановление затрагивает объект с ручным подтверждением")
|
||||
return { connection, ids }
|
||||
}
|
||||
try {
|
||||
@@ -145,7 +147,7 @@ async function executeRestore(operationId: string, principal: McpPrincipal,origi
|
||||
if (createHash("sha256").update(buffer).digest("hex") !== source.sha256) throw new McpError("STATE_CONFLICT", "Содержимое архива изменилось", 409)
|
||||
if (!(await pool.query("UPDATE mcp_operations SET state='running' WHERE id=$1 AND state='pending' RETURNING id", [operationId])).rowCount) throw new McpError("STATE_CONFLICT", "Операция уже остановлена", 409)
|
||||
stage = "restore"
|
||||
const outcome = await withVerifiedConfigurationChange({ actorId: connection.owner_id, connectionId: connection.id, serverIds: ids, objects: evidence.objects, approved: true, serverWide: true }, () => restoreSystemDatabaseBackup(buffer, { beforeRestore: async () => {
|
||||
const outcome = await withVerifiedConfigurationChange({ actorId: connection.owner_id, connectionId: connection.id, serverIds: ids, objects: evidence.objects, approved: true, approvalSource: plan.evidence.approvalSource, serverWide: true }, () => restoreSystemDatabaseBackup(buffer, { beforeRestore: async () => {
|
||||
await assertAuthority(); await archive(payload.archiveRef, connection.owner_id)
|
||||
await durableWrite(filename(source.id, ".used"), JSON.stringify({ operationId, at: new Date().toISOString() }), true)
|
||||
await assertOriginalMcpTemporaryGrant(pool,principal.connectionId,principal.credentialId,originalGrant)
|
||||
|
||||
@@ -8,7 +8,7 @@ import { decryptSecret, decryptSecretExisting, fingerprint, privateFingerprinter
|
||||
const MAX_BYTES = 2097152
|
||||
const uuid = z.string().uuid()
|
||||
const deny = (): never => { throw new McpError("STATE_CONFLICT", "Исходная привязка отмены диагностики не подтверждена", 409) }
|
||||
const sealedSchema = z.object({ sealed: z.string().min(1).max(MAX_BYTES) }).strict()
|
||||
const sealedSchema = z.object({ sealed: z.string().min(1).max(MAX_BYTES), approvalSource: z.literal("connection-policy").optional() }).strict()
|
||||
interface SourceCrypto { open(value: string): Promise<string>; proof(value: unknown): Promise<string> }
|
||||
export interface DiagnosticCancellationOwner { connectionId: string; ownerId: string }
|
||||
export interface DiagnosticCancellationSource {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { z } from "zod"
|
||||
import { flowAnalyticsDtoSchema } from "@mmapp/contracts/traffic-flow"
|
||||
import { buildFlowAnalytics } from "../../services/traffic-flow-analytics.js"
|
||||
import { readFlowSourceLimits } from "../../services/traffic-flow-source-limits.js"
|
||||
import { readFlowWindow } from "../../services/traffic-flow-read-window.js"
|
||||
import { getBoundedFlowServerRowById as getServerRowById } from "../../services/traffic-flow-server-source.js"
|
||||
import { assertTrafficHistoryAccess } from "./traffic-history-actions.js"
|
||||
import { McpError } from "./security.js"
|
||||
@@ -20,10 +21,11 @@ export function registerLegacyFlowAnalyticsAction(register: typeof add) {
|
||||
if (!server) throw new McpError("NOT_FOUND", "Сервер не найден", 404)
|
||||
const authorize = () => assertTrafficHistorySource(context, server)
|
||||
try {
|
||||
const result = await buildFlowAnalytics({ serverId: server.id, minutes: Number(input.minutes), iface: input.interface as string | undefined, skipHeavy: true },
|
||||
{ serverIds: context.connection.policy.scope.serverIds, maxRows: 20000, authorize })
|
||||
const result = await readFlowWindow(Number(input.minutes), minutes => buildFlowAnalytics({ serverId: server.id, minutes, iface: input.interface as string | undefined, skipHeavy: true },
|
||||
{ serverIds: context.connection.policy.scope.serverIds, maxRows: 20000, authorize }), context.signal)
|
||||
return {
|
||||
serverId: input.serverId, source: "PostgreSQL/flow_buckets", observedAt: new Date().toISOString(),
|
||||
serverId: input.serverId, source: "PostgreSQL/flow_buckets", observedAt: new Date().toISOString(), sourceWindow: result.sourceWindow,
|
||||
timeBasis: result.timeBasis, admittedTotals: result.admittedTotals,
|
||||
bpsNow: result.bpsNow, bytes: result.bytes, packets: result.packets, conversations: result.conversations,
|
||||
uniqueSrc: result.uniqueSrc, uniqueDst: result.uniqueDst, rxSeries: result.rxSeries, txSeries: result.txSeries,
|
||||
applications: result.applications, protocols: result.protocols, countries: result.countries,
|
||||
@@ -57,23 +59,24 @@ export function registerFlowAnalyticsActions(register: typeof add) {
|
||||
try {
|
||||
const limits = await readFlowSourceLimits()
|
||||
await authorize()
|
||||
const result = flowAnalyticsDtoSchema.parse(await buildFlowAnalytics({ serverId: server.id, minutes: Number(input.minutes), userId: input.userId as string | undefined,
|
||||
const { sourceWindow, ...raw } = await readFlowWindow(Number(input.minutes), minutes => buildFlowAnalytics({ serverId: server.id, minutes, userId: input.userId as string | undefined,
|
||||
iface: input.interface as string | undefined, dedup: input.dedup === true, excludeMesh: input.excludeMesh === true, excludeOverlay: input.excludeOverlay === true },
|
||||
{ serverIds: context.connection.policy.scope.serverIds, maxRows: 20000, authorize }))
|
||||
{ serverIds: context.connection.policy.scope.serverIds, maxRows: 20000, authorize }), context.signal)
|
||||
const result = flowAnalyticsDtoSchema.parse(raw)
|
||||
const offset = Number(input.offset), limit = Number(input.limit), pages: Record<string, { total: number; nextOffset: number | null }> = {}
|
||||
const analytics: Record<string, unknown> = { ...result }
|
||||
for (const [key, value] of Object.entries(result)) if (Array.isArray(value) && !["rxSeries", "txSeries"].includes(key)) {
|
||||
analytics[key] = value.slice(offset, offset + limit)
|
||||
pages[key] = { total: value.length, nextOffset: offset + limit < value.length ? offset + limit : null }
|
||||
}
|
||||
// Ring rates are server context, including when the retained rows are filtered by user/interface.
|
||||
// Wire rate uses interface counters. Keep rate estimates separate from retained volume.
|
||||
const { bpsNow: _bpsNow, bpsWire: _bpsWire, bpsOverlay: _bpsOverlay, bytesWire: _bytesWire, live: _live, ...retained } = analytics
|
||||
await authorize()
|
||||
if ((await readFlowSourceLimits()).topN !== limits.topN) throw new McpError("STATE_CONFLICT", "Предел источника NetFlow изменился во время чтения", 409)
|
||||
await authorize()
|
||||
const output = { serverId: server.id, minutes: Number(input.minutes), interface: input.interface ?? null, userId: input.userId ?? null,
|
||||
const output = { serverId: server.id, minutes: sourceWindow?.effectiveMinutes ?? Number(input.minutes), sourceWindow, interface: input.interface ?? null, userId: input.userId ?? null,
|
||||
analytics: retained, pages, contextRates: { bpsNowEstimate: _bpsNow, bpsWireEstimate: _bpsWire, bpsOverlayEstimate: _bpsOverlay, bytesWireEstimate: _bytesWire,
|
||||
listenerBound: _live, userAttributionVerified: false, semantics: "REST estimates: current server/interface ring with retained-window fallback; wire projection uses current interface snapshots" },
|
||||
listenerBound: _live, userAttributionVerified: false, semantics: "REST estimates: filtered or overlay-inclusive rates use the latest complete retained minute with a window-average fallback; wire uses current interface snapshots" },
|
||||
readStartedAt: startedAt, observedAt: new Date().toISOString(), live: false,
|
||||
source: "retained flow_buckets plus pending; same REST analytics engine with scoped topology",
|
||||
rowLimit: 20000, topRowsPerBucketLimit: Math.max(20, limits.topN), engineTopListLimit: Math.min(50, Math.max(10, limits.topN)),
|
||||
|
||||
@@ -4,6 +4,7 @@ import { z } from "zod"
|
||||
import { readFlowExporterInputs, readFlowClientInputs, observeFlowExporter, observeFlowClient } from "../../services/traffic-flow-catalog-read.js"
|
||||
import { getFlowMonthly } from "../../services/traffic-flow-analytics.js"
|
||||
import { readFlowCatalogWindow } from "../../services/traffic-flow-catalog-read.js"
|
||||
import { readFlowWindow } from "../../services/traffic-flow-read-window.js"
|
||||
import { projectFlowTalkers } from "../../services/traffic-flow-talkers-model.js"
|
||||
import { resolveIfaceName } from "../../services/traffic-flow-ifindex.js"
|
||||
import { assertTrafficHistoryAccess } from "./traffic-history-actions.js"
|
||||
@@ -21,9 +22,10 @@ export function registerFlowCatalogActions(register: typeof add) {
|
||||
await assertTrafficHistoryAccess(context)
|
||||
const { selected, ...page } = await catalogDevices(input, context, { includeDisabled: true })
|
||||
try {
|
||||
const window = await readFlowCatalogWindow(Number(input.minutes), selected.map(row => row.id))
|
||||
const requestedMinutes = Number(input.minutes)
|
||||
const window = await readFlowWindow(requestedMinutes, minutes => readFlowCatalogWindow(minutes, selected.map(row => row.id)), context.signal)
|
||||
const topN = window.topConversationsLimit ?? 20
|
||||
const projection = projectFlowTalkers(window.rows, Number(input.minutes), topN, new Map(selected.map(row => [row.id, row.name])), resolveIfaceName)
|
||||
const projection = projectFlowTalkers(window.rows, window.sourceWindow?.effectiveMinutes ?? requestedMinutes, topN, new Map(selected.map(row => [row.id, row.name])), resolveIfaceName)
|
||||
const offset = Number(input.offset), limit = Number(input.limit), items = projection.talkers.slice(offset, offset + limit)
|
||||
for (const server of selected) await assertTrafficHistorySource(context, server)
|
||||
if (selected.length && (await getTrafficFlowSettingsRow()).topN !== topN) throw new McpError("STATE_CONFLICT", "Предел источника NetFlow изменился во время чтения", 409)
|
||||
@@ -33,7 +35,7 @@ export function registerFlowCatalogActions(register: typeof add) {
|
||||
retainedRowsBytes: projection.retainedRowsBytes, observedExporters: projection.observedExporters,
|
||||
uniqueSrc: projection.uniqueSrc, uniqueDst: projection.uniqueDst, topProto: projection.topProto,
|
||||
topConversationsLimit: topN, topRowsPerBucketLimit: window.topRowsPerBucketLimit,
|
||||
incomplete: true, from: window.since, to: window.until,
|
||||
incomplete: true, from: window.since, to: window.until, sourceWindow: window.sourceWindow,
|
||||
source: "flow_buckets plus pending; scope before SQL and retained conversation aggregation",
|
||||
bpsMeaning: "retained conversation bytes averaged over the entire requested window",
|
||||
...sourceSemantics },context)
|
||||
@@ -47,27 +49,31 @@ export function registerFlowCatalogActions(register: typeof add) {
|
||||
register("traffic.flow.exporters.read", "mm_netflow_exporters_get", "NetFlow: экспортёры выбранной области", "traffic", "mm:traffic:read", "read", "servers", schema, {
|
||||
read: async (input, context) => {
|
||||
const { selected, ...page } = await catalogDevices(input, context, { includeDisabled: true })
|
||||
const data = await readFlowExporterInputs(Number(input.minutes), selected.map(row => row.id))
|
||||
const requestedMinutes = Number(input.minutes)
|
||||
const data = await readFlowWindow(requestedMinutes, minutes => readFlowExporterInputs(minutes, selected.map(row => row.id)), context.signal)
|
||||
const present = new Set([...data.window.rows.map(row => row.serverId), ...data.configuredIds])
|
||||
const all = data.catalog.list.filter(row => present.has(row.id)).sort((a, b) => a.id - b.id)
|
||||
const offset = Number(input.offset), limit = Number(input.limit)
|
||||
return withCurrentFlowReadQuality({ ...page, exporters: all.slice(offset, offset + limit).map(row => observeFlowExporter(data, row.id, Number(input.minutes))), total: all.length,
|
||||
nextOffset: offset + limit < all.length ? offset + limit : null, incomplete: page.incomplete || all.length > limit || offset > 0 || data.configurationMembershipVerified === false,
|
||||
for (const server of selected) await assertTrafficHistorySource(context, server)
|
||||
return withCurrentFlowReadQuality({ ...page, exporters: all.slice(offset, offset + limit).map(row => observeFlowExporter(data, row.id, data.sourceWindow?.effectiveMinutes ?? requestedMinutes)), total: all.length,
|
||||
nextOffset: offset + limit < all.length ? offset + limit : null, incomplete: !!data.sourceWindow || page.incomplete || all.length > limit || offset > 0 || data.configurationMembershipVerified === false,
|
||||
configurationMembershipVerified: data.configurationMembershipVerified, configurationEvidence: "stored-peer-records; device exporter settings and keys not verified", source: "flow_buckets plus pending; retained top rows per server/bucket",
|
||||
failures: data.configurationMembershipVerified === false ? [{ source: "selected-peer-membership", code: "UNVERIFIED_SCHEMA" }] : [],
|
||||
topRowsPerBucketLimit: data.window.topRowsPerBucketLimit, from: data.window.since, to: data.window.until, ...sourceSemantics },context)
|
||||
topRowsPerBucketLimit: data.window.topRowsPerBucketLimit, from: data.window.since, to: data.window.until, sourceWindow: data.sourceWindow, ...sourceSemantics },context)
|
||||
},
|
||||
})
|
||||
register("traffic.flow.clients.read", "mm_netflow_clients_get", "NetFlow: клиенты по доступным привязкам", "traffic", "mm:traffic:read", "read", "servers", schema.safeExtend({ limit: z.number().int().min(1).max(50).default(20) }), {
|
||||
read: async (input, context) => {
|
||||
const { selected, ...page } = await catalogDevices(input, context, { includeDisabled: true })
|
||||
const offset = Number(input.offset), limit = Number(input.limit)
|
||||
const data = await readFlowClientInputs(Number(input.minutes), { serverIds: selected.map(row => row.id), allowedServerIds: context.connection.policy.scope.serverIds, offset, limit })
|
||||
const clients = data.users.sort((a, b) => a.id.localeCompare(b.id)).map(user => observeFlowClient(user, data.window.rows, Number(input.minutes)))
|
||||
const requestedMinutes = Number(input.minutes)
|
||||
const data = await readFlowWindow(requestedMinutes, minutes => readFlowClientInputs(minutes, { serverIds: selected.map(row => row.id), allowedServerIds: context.connection.policy.scope.serverIds, offset, limit }), context.signal)
|
||||
const clients = data.users.sort((a, b) => a.id.localeCompare(b.id)).map(user => observeFlowClient(user, data.window.rows, data.sourceWindow?.effectiveMinutes ?? requestedMinutes))
|
||||
for (const server of selected) await assertTrafficHistorySource(context, server)
|
||||
return withCurrentFlowReadQuality({ ...page, clients, total: data.total, nextOffset: offset + clients.length < data.total ? offset + clients.length : null,
|
||||
incomplete: page.incomplete || clients.length < data.total || clients.some(row => !row.completeSourceSet || !row.completeAttribution),
|
||||
incomplete: !!data.sourceWindow || page.incomplete || clients.length < data.total || clients.some(row => !row.completeSourceSet || !row.completeAttribution),
|
||||
source: "retained ingress rows and current named whole-interface bindings", topRowsPerBucketLimit: data.window.topRowsPerBucketLimit,
|
||||
from: data.window.since, to: data.window.until, ...sourceSemantics },context)
|
||||
from: data.window.since, to: data.window.until, sourceWindow: data.sourceWindow, ...sourceSemantics },context)
|
||||
},
|
||||
})
|
||||
register("traffic.flow.monthly.read", "mm_netflow_monthly_report_get", "NetFlow: месячный отчёт выбранной области", "traffic", "mm:traffic:read", "read", "servers", catalogPaging.safeExtend({ month: z.string().regex(/^(?!0000)\d{4}-(0[1-9]|1[0-2])$/) }), {
|
||||
|
||||
@@ -57,7 +57,7 @@ export function registerFlowLiveActions(register: typeof add) {
|
||||
source: "REST live sample engine: current ring estimates and rolling flow_buckets plus pending; uncached scoped reads",
|
||||
rowLimit: 20000, incomplete: true, coverageVerified: false, forwardingVerified: false, rowSumIsNetworkTotal: false,
|
||||
historicalOwnershipVerified: false, currentTrafficVerified: false,
|
||||
rateSemantics: "REST live rates: current server/interface ring; retained-window fallback; user filter does not attribute ring rates",
|
||||
rateSemantics: "REST live rates: filtered or overlay-inclusive rates use the latest complete retained minute with a window-average fallback",
|
||||
liveMeaning: "events[].data.live means listener bound; it does not prove exporter freshness or forwarding",
|
||||
attribution: "current named whole-interface bindings; users with hidden bindings excluded",
|
||||
collectorStarted: false, backgroundEnrichmentStarted: false }
|
||||
|
||||
@@ -5,6 +5,7 @@ import { flowMapHopsDtoSchema } from "@mmapp/contracts/traffic-flow"
|
||||
import { isDeepStrictEqual } from "node:util"
|
||||
import { buildFlowMapHops } from "../../services/traffic-flow-map-hops.js"
|
||||
import { readFlowMapSourceLimits } from "../../services/traffic-flow-source-limits.js"
|
||||
import { readFlowWindow } from "../../services/traffic-flow-read-window.js"
|
||||
import { getBoundedFlowServerRowById as getServerRowById } from "../../services/traffic-flow-server-source.js"
|
||||
import { assertTrafficHistoryAccess } from "./traffic-history-actions.js"
|
||||
import { McpError } from "./security.js"
|
||||
@@ -27,9 +28,10 @@ export function registerFlowMapActions(register: typeof add) {
|
||||
try {
|
||||
const limits = await readFlowMapSourceLimits()
|
||||
await authorize()
|
||||
const result = flowMapHopsDtoSchema.parse(await buildFlowMapHops({ serverId: server.id, minutes: Number(input.minutes), userId: input.userId as string | undefined,
|
||||
const { sourceWindow, ...raw } = await readFlowWindow(Number(input.minutes), minutes => buildFlowMapHops({ serverId: server.id, minutes, userId: input.userId as string | undefined,
|
||||
iface: input.interface as string | undefined, dedup: input.dedup === true, excludeMesh: input.excludeMesh === true, excludeOverlay: input.excludeOverlay === true },
|
||||
{ serverIds: context.connection.policy.scope.serverIds, maxRows: 20000, authorize }))
|
||||
{ serverIds: context.connection.policy.scope.serverIds, maxRows: 20000, authorize }), context.signal)
|
||||
const result = flowMapHopsDtoSchema.parse(raw)
|
||||
const pages: Record<string, { total: number; nextOffset: number | null }> = {}
|
||||
const project = (value: unknown, path = "", depth = 0): unknown => {
|
||||
if (Array.isArray(value)) {
|
||||
@@ -45,7 +47,7 @@ export function registerFlowMapActions(register: typeof add) {
|
||||
await authorize()
|
||||
if (!isDeepStrictEqual(limits, await readFlowMapSourceLimits())) throw new McpError("STATE_CONFLICT", "Пределы карты NetFlow изменились во время чтения", 409)
|
||||
await authorize()
|
||||
const output = { serverId: server.id, userId: input.userId ?? null, interface: input.interface ?? null, map: projected, pages,
|
||||
const output = { serverId: server.id, userId: input.userId ?? null, interface: input.interface ?? null, map: projected, pages, sourceWindow,
|
||||
readStartedAt: startedAt, observedAt: new Date().toISOString(), live: false, listenerBound,
|
||||
source: "retained flow_buckets plus pending; same REST map engine with scoped topology",
|
||||
topRowsPerBucketLimit: Math.max(20, limits.topN), rowLimit: 20000, incomplete: true, graphComplete: false,
|
||||
|
||||
@@ -14,6 +14,7 @@ import { createMcpConnection, authenticateMcpToken, updateMcpSettings, rotateCon
|
||||
import { invokeMcpTool, approvePlan, applyPlan, recoverMcpOperations } from "./runtime.js"
|
||||
import { decryptSecret, encryptSecret, invalidateMcpAccess, tokenDigest } from "./security.js"
|
||||
import { addRoutingDependencies } from "./routing-dependencies.js"
|
||||
import { FlowSourceWindowTooLarge } from "../../services/traffic-flow-read-window.js"
|
||||
import { acceptOspfRouteProofMcp } from "./ospf-route-proof-acceptance.js"
|
||||
import { acceptNetworkNeighborsMcp } from "./network-neighbors-acceptance.js"
|
||||
import { acceptIpv6NdWriteMcp } from "./ipv6-nd-write-acceptance.js"
|
||||
@@ -1629,13 +1630,33 @@ try {
|
||||
await assert.rejects(invokeMcpTool(ospfPrincipal, "mm_ospf_instance_create_plan", { serverId: 1, fields: { name: "blocked-ospf", "router-id": "192.0.2.1", version: "3", vrf: "main" } }))
|
||||
await pool.query("DELETE FROM mcp_protections WHERE id=$1", [vrfProtection])
|
||||
bgpFixtures = true
|
||||
const bgpAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "BGP dependency test", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:bgp:read", "mm:bgp:write", "mm:network:read"], scope: { serverIds: [1], operationIds: ["ros.bgp_instance.delete", "ros.bgp_instance.update", "ros.bgp_connection.create", "ros.vrf.delete"], allowGlobal: false } }), actor)
|
||||
const bgpAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "BGP dependency test", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:bgp:read", "mm:bgp:write", "mm:network:read"], scope: { serverIds: [1], operationIds: ["ros.bgp_instance.delete", "ros.bgp_instance.update", "ros.bgp_connection.create", "ros.bgp_connection.update", "ros.vrf.delete"], allowGlobal: false } }), actor)
|
||||
const bgpPrincipal = await authenticateMcpToken(bgpAccess.token)
|
||||
await assert.rejects(invokeMcpTool(bgpPrincipal, "mm_bgp_instance_delete_plan", { serverId: 1, id: "*bgp" }), /используется подключением/)
|
||||
const peerFields = { name: "new-peer", instance: "missing", "remote.address": "192.0.2.2", "local.address": "192.0.2.1", "remote.as": 64513, "local.role": "ebgp" }
|
||||
await assert.rejects(invokeMcpTool(bgpPrincipal, "mm_bgp_connection_create_plan", { serverId: 1, fields: peerFields }), /Не подтверждена зависимость/)
|
||||
await assert.rejects(invokeMcpTool(bgpPrincipal, "mm_bgp_connection_create_plan", { serverId: 1, fields: { ...peerFields, instance: "fixture-bgp", "local.address": "fd00::1" } }), /одного семейства/)
|
||||
await assert.rejects(invokeMcpTool(bgpPrincipal, "mm_bgp_connection_create_plan", { serverId: 1, fields: { ...peerFields, instance: "fixture-bgp", "remote.address": "0.0.0.0/0" } }))
|
||||
const savedPeer = fixtureBgp.connection[0]
|
||||
const preparePassivePeer = () => invokeMcpTool(bgpPrincipal, "mm_bgp_connection_update_plan", {
|
||||
serverId: 1, id: savedPeer[".id"], fields: { connect: false, listen: true },
|
||||
}) as Promise<{ state: string; changes: { steps: { step: { fields: unknown } }[] } }>
|
||||
try {
|
||||
for (const [local, remote] of [["10.255.255.100", "10.255.255.11/32"], ["2001:db8::1", "2001:db8::2/128"]]) {
|
||||
fixtureBgp.connection[0] = { ...savedPeer, "local.address": local, "remote.address": remote }
|
||||
const plan = await preparePassivePeer()
|
||||
assert.equal(plan.state, "awaiting-approval")
|
||||
assert.equal(plan.changes.steps.length, 1)
|
||||
assert.deepEqual(plan.changes.steps[0].step.fields, { connect: "false", listen: "true" })
|
||||
}
|
||||
fixtureBgp.connection[0] = { ...savedPeer, "remote.address": "2001:db8::2/128" }
|
||||
await assert.rejects(preparePassivePeer(), /одного семейства/)
|
||||
fixtureBgp.connection[0] = { ...savedPeer, "remote.address": "192.0.2.2/33" }
|
||||
await assert.rejects(preparePassivePeer(), /Некорректный адрес или префикс/)
|
||||
} finally {
|
||||
fixtureBgp.connection[0] = savedPeer
|
||||
}
|
||||
console.log("✓ MCP BGP dependency validation accepts stored IPv4/IPv6 CIDR for a passive-mode update and rejects mixed families and invalid prefix lengths")
|
||||
const peerProtection = randomUUID()
|
||||
await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [peerProtection, JSON.stringify({ kind: "bgp_connection", id: "*peer", serverId: 1, protection: "deny" })])
|
||||
await assert.rejects(invokeMcpTool(bgpPrincipal, "mm_bgp_instance_update_plan", { serverId: 1, id: "*bgp", fields: { as: 64514 } }))
|
||||
@@ -2033,7 +2054,7 @@ try {
|
||||
assert.equal(badPeer.configurationMembershipVerified,false); assert.equal(badPeer.incomplete,true); assert.equal(badPeer.failures.length,1); assert.equal(badPeer.exporters[0].storedPeerRecord,null)
|
||||
await pool.query("UPDATE traffic_flow_settings SET peers_json=jsonb_set(peers_json,'{0,publicKey}',$1::jsonb) WHERE id=1",[JSON.stringify("public-one")]); invalidateFlowSettings()
|
||||
const rawWindow = await flowWindow(5,[1]); assert.equal(rawWindow.rows.length,4); assert.ok(rawWindow.rows.every(row=>row.serverId===1&&row.bucketAt<=rawWindow.until))
|
||||
await assert.rejects(flowRows(rawWindow.since,1,{maxRows:1,untilIso:rawWindow.until}),/предел/)
|
||||
await assert.rejects(flowRows(rawWindow.since,1,{maxRows:1,untilIso:rawWindow.until}),FlowSourceWindowTooLarge)
|
||||
type TalkerPage = { items: {serverId:string;src:string;bytes:number;bps:number}[]; total:number; totalConversations:number; retainedRowsBytes:number; nextOffset:number|null; incomplete:boolean; serverIds:number[] }
|
||||
const talkers = await invokeMcpTool(flowPrincipal,"mm_netflow_talkers_get",{minutes:5,limit:1}) as TalkerPage
|
||||
assert.equal(talkers.retainedRowsBytes,3100); assert.equal(talkers.totalConversations,4); assert.equal(talkers.total,4); assert.equal(talkers.nextOffset,1); assert.equal(talkers.incomplete,true)
|
||||
@@ -2099,8 +2120,14 @@ try {
|
||||
assert.ok(restVisible.every((row:{src:string})=>row.src!=="198.18.242.6"),"Future rows must not enter REST talkers")
|
||||
assert.equal((await flowHttp.inject({method:"GET",url:"/api/traffic/flows?range=5m"})).statusCode,200)
|
||||
for(const path of ["/api/traffic/flow/exporters?range=5m","/api/traffic/flow/clients?range=5m","/api/traffic/flow/monthly?month="+flowMonth+"&serverId=1"]) { const response = await flowHttp.inject({method:"GET",url:path}); assert.equal(response.statusCode,200,response.body); assert.equal(typeof response.json(),"object") } } finally { await flowHttp.close() }
|
||||
await pool.query("INSERT INTO flow_buckets(server_id,bucket_at,src,dst,bytes,packets,in_iface) SELECT 1,now()-interval '90 seconds'+n*interval '1 millisecond','198.18.242.11','8.8.8.8',1,1,'cap' FROM generate_series(1,50001) n")
|
||||
await assert.rejects(invokeMcpTool(flowPrincipal,"mm_netflow_talkers_get",{minutes:5}), (error:{code?:string})=>error.code==="STATE_UNAVAILABLE")
|
||||
await pool.query("INSERT INTO flow_buckets(server_id,bucket_at,src,dst,bytes,packets,in_iface) SELECT 1,now()-interval '90 seconds'+n*interval '1 microsecond','198.18.242.11','8.8.8.8',1,1,'cap' FROM generate_series(1,50001) n")
|
||||
const denseReport = await invokeMcpTool(flowPrincipal, "mm_netflow_talkers_get", { minutes: 5 }) as TalkerPage & {
|
||||
sourceWindow: { requestedMinutes: number; effectiveMinutes: number }; from: string; to: string
|
||||
}
|
||||
assert.equal(denseReport.sourceWindow.requestedMinutes, 5)
|
||||
assert.equal(denseReport.sourceWindow.effectiveMinutes, 1)
|
||||
assert.equal(Date.parse(denseReport.to) - Date.parse(denseReport.from), denseReport.sourceWindow.effectiveMinutes * 60_000)
|
||||
assert(denseReport.items.every(row => row.src !== "198.18.242.11"), "The skipped dense window must not publish a partial page")
|
||||
await pool.query("DELETE FROM flow_buckets WHERE server_id=1 AND src='198.18.242.11'")
|
||||
const talkerRace = async (change:()=>Promise<unknown>, principal=flowPrincipal, signal?:AbortSignal) => {
|
||||
const blocker = await pool.connect()
|
||||
@@ -2134,7 +2161,7 @@ try {
|
||||
assert.equal(catalogCalls,0,"Talker history and races must never poll RouterOS")
|
||||
assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1",[flowAccess.connection.id])).rows[0].n,0)
|
||||
assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1",[flowAccess.connection.id])).rows).includes("NEVER_FLOW"))
|
||||
console.log("✓ NetFlow talkers: shared REST parity, scoped SQL/pending, topN/pages, 50001-row refusal, real blocked-read source/topN/grant/owner/cancel/pause races, no global fields or network")
|
||||
console.log("✓ NetFlow talkers: shared REST parity, scoped SQL/pending, topN/pages, bounded dense-window fallback, real blocked-read source/topN/grant/owner/cancel/pause races, no global fields or network")
|
||||
} finally {
|
||||
countCatalogCalls = false; clearPendingFlows(); resetFlowRipe()
|
||||
await pool.query("DELETE FROM app_users WHERE id=ANY($1::text[])",[flowUsers])
|
||||
@@ -3891,6 +3918,55 @@ try {
|
||||
assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='owner.revoke' AND payload->>'eventId'=$1", [event.eventId])).rows[0].n, 1)
|
||||
delete process.env.MCP_PORTAL_WEBHOOK_SECRET
|
||||
console.log("✓ Signed portal revocation: tamper/stale rejection, one atomic event, revoked credentials and stale-JWT regrant prevention")
|
||||
{
|
||||
const serviceInput = mcpConnectionCreateSchema.parse({
|
||||
name: "Announced service plans", authority: "service", allowUnattendedPlans: true, mode: "autonomous", expiresAt: null,
|
||||
permissions: ["mm:network:write", "mm:settings:admin"],
|
||||
scope: { serverIds: (await pool.query<{ id: number }>("SELECT id FROM servers")).rows.map(({ id }) => id),
|
||||
allowGlobal: true, operationIds: ["ros.ip_settings.update", "logs.settings.update", "changes.read", "changes.apply"] },
|
||||
limits: { mutationsPerHour: 500, objectsPerPlan: 500, serversPerPlan: 100 },
|
||||
})
|
||||
const { token } = await createMcpConnection(serviceInput, actor)
|
||||
const service = await authenticateMcpToken(token)
|
||||
const prepare = () => invokeMcpTool(service, "mm_ip_settings_update_plan", { serverId: 1, fields: { "rp-filter": "loose" } }) as Promise<{ planId: string; state: string; announcement?: { digest: string } }>
|
||||
const beforeWrites = writes
|
||||
const first = await prepare()
|
||||
assert.equal(first.state, "draft")
|
||||
assert.match(first.announcement!.digest, /^[a-f0-9]{64}$/)
|
||||
const inspected = await invokeMcpTool(service, "mm_change_get", { planId: first.planId }) as { announcement: { digest: string } }
|
||||
assert.equal(inspected.announcement.digest, first.announcement!.digest)
|
||||
const apply = (planId: string, digest?: string) => invokeMcpTool(service, "mm_change_apply", {
|
||||
planId, idempotencyKey: randomUUID(), ...(digest && { announcedPlanDigest: digest }),
|
||||
}) as Promise<{ id: string }>
|
||||
const denied = { code: "APPROVAL_REQUIRED" }
|
||||
await assert.rejects(apply(first.planId), denied)
|
||||
const second = await prepare()
|
||||
await assert.rejects(apply(first.planId, second.announcement!.digest), denied)
|
||||
const protection = randomUUID()
|
||||
await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [protection, JSON.stringify({ kind: "server", id: "1", serverId: 1, protection: "human-approval-required" })])
|
||||
try {
|
||||
await assert.rejects(apply(first.planId, first.announcement!.digest), denied)
|
||||
const protectedPlan = await prepare()
|
||||
assert.equal(protectedPlan.state, "awaiting-approval")
|
||||
assert.equal(protectedPlan.announcement, undefined)
|
||||
assert.equal(writes, beforeWrites)
|
||||
} finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) }
|
||||
const operation = await apply(first.planId, first.announcement!.digest)
|
||||
await awaitOperation(operation.id, "applied")
|
||||
assert.equal(writes, beforeWrites + 1)
|
||||
const publicResult = await invokeMcpTool(service, "mm_change_get", { planId: first.planId }) as { approvalSource: string; approvedBy: string | null }
|
||||
assert.equal(publicResult.approvalSource, "connection-policy")
|
||||
assert.equal(publicResult.approvedBy, null)
|
||||
const { rows: [start] } = await pool.query("SELECT payload FROM mcp_audit WHERE action='operation.start' AND payload->>'planId'=$1", [first.planId])
|
||||
assert.equal(start.payload.announcedPlanDigest, first.announcement!.digest)
|
||||
assert.equal((await pool.query("SELECT 1 FROM mcp_audit WHERE action='plan.approve' AND payload->>'planId'=$1", [first.planId])).rowCount, 0)
|
||||
const { getLogSettings } = await import("../../services/router-logs-collector.js")
|
||||
const retentionDays = Math.min(365, (await getLogSettings()).retentionDays + 1)
|
||||
const global = await invokeMcpTool(service, "mm_logs_settings_update_plan", { settings: { retentionDays } }) as { planId: string; announcement: { digest: string } }
|
||||
await awaitOperation((await apply(global.planId, global.announcement.digest)).id, "applied")
|
||||
assert.equal((await getLogSettings()).retentionDays, retentionDays)
|
||||
console.log("✓ Unattended MCP checks exact plan digest, current protections and delegated provenance for RouterOS and application writes")
|
||||
}
|
||||
await setConnectionStatus(issued.connection.id, "paused", actor)
|
||||
await assert.rejects(authenticateMcpToken(rotated.token))
|
||||
await setConnectionStatus(issued.connection.id, "active", actor)
|
||||
|
||||
@@ -62,11 +62,17 @@ const router = createServer(async (req, res) => {
|
||||
if (req.method === "GET") { res.end(JSON.stringify(catalogs[path] ?? [])); return }
|
||||
let raw = ""; for await (const chunk of req) raw += chunk
|
||||
const fields = raw ? JSON.parse(raw) as Row : {}
|
||||
if (path === "/file/get" || path === "/file/print") { res.end(JSON.stringify({ ret: fakeP12 })); return }
|
||||
if (path === "/file/get") {
|
||||
assert(catalogs["/file"].some(row => row[".id"] === fields[".id"] && row[".id"].startsWith("**")))
|
||||
res.end(JSON.stringify({ ret: fakeP12 }))
|
||||
return
|
||||
}
|
||||
commands.push({ method: req.method!, path, ...(fields.name ? { name: fields.name } : {}) })
|
||||
if (req.method === "POST" && path === "/certificate/export-certificate") {
|
||||
assert(catalogs["/certificate"].some(row => row.name === fields.name))
|
||||
catalogs["/file"].push({ ".id": `*new${++serial}`, name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(fakeP12)), type: "file" })
|
||||
const certificate = catalogs["/certificate"].find(row => row[".id"] === fields[".id"])
|
||||
assert(certificate && !("name" in fields))
|
||||
commands.at(-1)!.name = certificate.name
|
||||
catalogs["/file"].unshift({ ".id": `**New${++serial}`, name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(fakeP12)), type: "file" })
|
||||
if (mode === "lost") { req.socket.destroy(); return }
|
||||
} else if (req.method === "DELETE" && path.startsWith("/file/")) {
|
||||
catalogs["/file"] = catalogs["/file"].filter(row => row[".id"] !== path.slice("/file/".length))
|
||||
|
||||
@@ -42,19 +42,30 @@ export async function acceptIpsecUserCreate(actor: AuthUser) {
|
||||
if (req.method === "GET") { res.end(JSON.stringify(catalogs[path] ?? [])); return }
|
||||
let raw = ""; for await (const chunk of req) raw += chunk
|
||||
const fields = raw ? JSON.parse(raw) as Row : {}
|
||||
if (path === "/file/get" || path === "/file/print") { res.end(JSON.stringify({ ret: fakeP12 })); return }
|
||||
if (path === "/file/get") {
|
||||
assert(catalogs["/file"].some(row => row[".id"] === fields[".id"] && row[".id"].startsWith("**")))
|
||||
res.end(JSON.stringify({ ret: mode === "download-failed" ? "short" : fakeP12 }))
|
||||
return
|
||||
}
|
||||
commands.push({ method: req.method!, path, fields })
|
||||
// RouterOS 7.24 (stand probe 2026-11-21): no `comment` on /certificate; sign and export by `.id` only, no `name`/`days-valid`.
|
||||
const refusal = req.method === "PUT" && path === "/certificate" && "comment" in fields ? "unknown parameter comment"
|
||||
: path === "/certificate/sign" && ("days-valid" in fields || "name" in fields) ? ("days-valid" in fields ? "unknown parameter days-valid" : "missing =.id=")
|
||||
: path === "/certificate/export-certificate" && "name" in fields ? "unknown parameter name" : undefined
|
||||
if (refusal) { res.statusCode = 400; res.end(JSON.stringify({ error: 400, message: "Bad Request", detail: refusal })); return }
|
||||
if ((mode === "export-refused" && path === "/certificate/export-certificate") || (mode === "cleanup-refused" && req.method === "DELETE" && path.startsWith("/file/"))) {
|
||||
res.statusCode = 400; res.end(JSON.stringify({ error: 400, detail: "NEVER_NATIVE_ERROR_DETAILS" })); return
|
||||
}
|
||||
let result: Row = {}
|
||||
if (mode !== "ignored") {
|
||||
if (path === "/certificate/sign") {
|
||||
const row = catalogs["/certificate"].find(row => row[".id"] === fields[".id"]); assert(row)
|
||||
Object.assign(row, { ca: fields.ca, "digest-algorithm": "sha256", fingerprint: "synthetic-fingerprint", "invalid-before": "2026-01-01", "invalid-after": "2030-01-01", "private-key": "true", "serial-number": "synthetic" })
|
||||
} else if (path === "/certificate/export-certificate") assert(catalogs["/certificate"].some(row => row[".id"] === fields[".id"])), catalogs["/file"].push({ ".id": `*new${++serial}`, name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(fakeP12)), type: "file" })
|
||||
} else if (path === "/certificate/export-certificate") {
|
||||
assert(catalogs["/certificate"].some(row => row[".id"] === fields[".id"]))
|
||||
catalogs["/file"].unshift({ ".id": `**New${++serial}`, name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(fakeP12)), type: "file" })
|
||||
if (mode === "other-file-changed") catalogs["/file"][1].size = "99"
|
||||
}
|
||||
else {
|
||||
const catalog = Object.keys(catalogs).find(value => path === value || path.startsWith(value + "/")); assert(catalog)
|
||||
if (req.method === "PUT") { result = { ".id": `*new${++serial}`, ...fields, "native-default": "NEVER_NATIVE_DEFAULT" }; catalogs[catalog].push(result) }
|
||||
@@ -101,6 +112,7 @@ export async function acceptIpsecUserCreate(actor: AuthUser) {
|
||||
const stored = (await query("SELECT * FROM mcp_artifacts WHERE id=$1", [saved.artifactId])).rows[0]; hidden(stored); assert.equal(stored.access_scope.kind, "ipsec-user-bundle"); assert(stored.expires_at.getTime() <= Date.now() + 900000)
|
||||
const download = await portal.inject({ url: saved.downloadPath, headers }); assert.equal(download.statusCode, 200, download.body); assert.equal(download.headers["cache-control"], "private, no-store")
|
||||
const bundle = download.json(); assert.equal(Buffer.from(bundle.contentB64, "base64").toString(), fakeP12); assert.equal(bundle.passphrase, "NEVER_EXPORT_PASSPHRASE")
|
||||
assert.equal(JSON.parse(bundle.sswanContent).local.p12, bundle.contentB64)
|
||||
assert.equal((await portal.inject({ url: saved.downloadPath, headers: { authorization: `Bearer ${token("other-owner")}` } })).statusCode, 403)
|
||||
assert.equal((await portal.inject({ url: saved.downloadPath, headers: { authorization: `Bearer ${token(actor.id, ["mm:network:read"])}` } })).statusCode, 403)
|
||||
const listing: { statusCode: number; body: string; json: () => { items: Array<{ id: string; artifactReceipts: Array<{ artifactId: string }> }> } } = await portal.inject({ url: `/api/mcp/operations?connectionId=${connectionId}`, headers }); assert.equal(listing.statusCode, 200, listing.body); hidden(listing.json()); assert(listing.json().items.find((row: { id: string }) => row.id === result.operation.id)!.artifactReceipts.some((row: { artifactId: string }) => row.artifactId === saved.artifactId))
|
||||
@@ -110,6 +122,29 @@ export async function acceptIpsecUserCreate(actor: AuthUser) {
|
||||
const response = await rest.inject({ method: "POST", url: "/api/ipsec/users", payload: { serverId, name: "Alice", authMethod, ...(staticIp ? { staticIp } : {}), ...(authMethod === "certificate" ? { daysValid: 365, passphrase: "NEVER_EXPORT_PASSPHRASE" } : { psk: "NEVER_PSK_SECRET", remoteId: "alice.example.test" }) } }); assert.equal(response.statusCode, 201, response.body)
|
||||
assert.deepEqual(normalize(commands.slice(restStart)), accepted)
|
||||
}
|
||||
reset(); mode = "download-failed"
|
||||
const failedExport = await rest.inject({ method: "POST", url: "/api/ipsec/users", payload: { serverId, name: "Alice", staticIp: "192.0.2.7" } })
|
||||
assert.equal(failedExport.statusCode, 502, failedExport.body)
|
||||
assert.equal(failedExport.json().state, "partial")
|
||||
assert.deepEqual(catalogs["/ip/ipsec/identity"], base["/ip/ipsec/identity"])
|
||||
assert.deepEqual(catalogs["/ip/ipsec/mode-config"], base["/ip/ipsec/mode-config"])
|
||||
reset(); mode = "other-file-changed"
|
||||
const unrelatedChange = await rest.inject({ method: "POST", url: "/api/ipsec/users", payload: { serverId, name: "Alice" } })
|
||||
assert.equal(unrelatedChange.statusCode, 502)
|
||||
assert.equal(unrelatedChange.json().completedCommands, 2)
|
||||
assert.equal(catalogs["/file"].length, 2, "failed proof must stop before deletion")
|
||||
for (const [fault, stage, completed] of [["export-refused", "экспорт .p12", 2], ["cleanup-refused", "удаление временного .p12", 3]] as const) {
|
||||
reset(); mode = fault
|
||||
const response = await rest.inject({ method: "POST", url: "/api/ipsec/users", payload: { serverId, name: "Alice", staticIp: "192.0.2.7" } })
|
||||
assert.equal(response.statusCode, 502)
|
||||
const error = response.json()
|
||||
assert.equal(error.completedCommands, completed)
|
||||
assert(error.error.includes(stage))
|
||||
assert(error.error.includes("HTTP 400"))
|
||||
hidden(error)
|
||||
assert.deepEqual(catalogs["/ip/ipsec/identity"], base["/ip/ipsec/identity"])
|
||||
assert.deepEqual(catalogs["/ip/ipsec/mode-config"], base["/ip/ipsec/mode-config"])
|
||||
}
|
||||
reset(); const rawStart = commands.length
|
||||
for (const request of [{ name: "Alice" }, { name: "Alice", authMethod: "pre-shared-key", psk: "NEVER_RAW" }, { name: "Alice", privateKeyExport: true, staticIp: "bad" }]) await assert.rejects(prepare(request))
|
||||
assert.equal(commands.length, rawStart)
|
||||
|
||||
@@ -148,5 +148,5 @@ export function createIpsecUserCreateAdapter(overrides: Partial<typeof productio
|
||||
export function registerIpsecUserCreateActions(register: typeof add) {
|
||||
const adapter = createIpsecUserCreateAdapter()
|
||||
registerMcpService(operation, adapter.service)
|
||||
register(operation, "mm_ipsec_user_create_plan", "Создать клиента IPsec с проверкой всех native-команд, явным приватным экспортом и managed-only историей", "network", "mm:network:write", "write", "servers", ipsecUserCreateActionSchema, { version: 3, prepare: adapter.prepare })
|
||||
register(operation, "mm_ipsec_user_create_plan", "Создать клиента IPsec с проверкой всех native-команд, явным приватным экспортом и managed-only историей", "network", "mm:network:write", "write", "servers", ipsecUserCreateActionSchema, { version: 4, prepare: adapter.prepare })
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import { assertNoOperationConfigurationLockContext } from "../../services/operat
|
||||
import { validatePrincipal, audit, type McpPrincipal, type StoredOperation } from "./store.js"
|
||||
import { decryptSecret, fingerprint, McpError, mcpStateDirectory, privateFingerprinterExisting } from "./security.js"
|
||||
import { findActionByOperation } from "./actions.js"
|
||||
import { evaluateMcpPolicy } from "./policy.js"
|
||||
import { evaluateMcpPolicy, hasUnattendedPlanAuthority, operationApprovalRequirement } from "./policy.js"
|
||||
import { changeMutationCost } from "./change-cost.js"
|
||||
import { assertOriginalMcpTemporaryGrant, type OriginalServiceGrant } from "./temporary-grant-origin.js"
|
||||
import type { ChangeEvidence } from "./action-types.js"
|
||||
@@ -24,7 +24,7 @@ export const MCP_OPERATION_PRIVATE_CLAIM_KEY = "__mmappMcpOperationClaimV1"
|
||||
export interface McpOperationExecutionMetadata {
|
||||
readonly operationId: string; readonly planId: string; readonly connectionId: string; readonly ownerId: string
|
||||
readonly operationName: string; readonly operationVersion: number; readonly policyVersion: number; readonly generation: string
|
||||
readonly phase: "forward"; readonly approved: boolean; readonly principal: Readonly<McpPrincipal>
|
||||
readonly phase: "forward"; readonly approved: boolean; readonly approvalSource?: "connection-policy"; readonly principal: Readonly<McpPrincipal>
|
||||
readonly sealedEvidenceProof: string; readonly sealedPayloadProof: string
|
||||
readonly originalTemporaryGrantProof: string; readonly originalTemporaryGrantExpiresAt: number | null; readonly originalExpiresAt: number
|
||||
readonly serverIds: readonly number[]; readonly resources: readonly McpObjectRef[]
|
||||
@@ -40,7 +40,7 @@ declare const admissionBrand: unique symbol
|
||||
export type McpOperationAdmissionStart = Readonly<{ [admissionStartBrand]: true }>
|
||||
export type McpOperationAdmission = Readonly<{ [admissionBrand]: true }>
|
||||
type Admission = { operationId: string; planId: string; connectionId: string; nonce: string; principal: Readonly<McpPrincipal>; grant: Readonly<OriginalServiceGrant>
|
||||
generation: string; directory: string; keyEpoch: string; operationName: string; operationVersion: number; policyVersion: number; evidenceProof: string; payloadProof: string }
|
||||
generation: string; directory: string; keyEpoch: string; operationName: string; operationVersion: number; policyVersion: number; evidenceProof: string; payloadProof: string; approvalSource?: "connection-policy" }
|
||||
const starts = new WeakMap<object, { principal: Readonly<McpPrincipal>; wall: number; mono: number; directory: string; generation: string; keyEpoch: string }>(), admissions = new WeakMap<object, Admission>()
|
||||
const deny = (): never => { throw new McpError("PERMISSION_DENIED", "Исходный MCP operation root недоступен", 403) }
|
||||
const purpose = "mcp-operation-execution-origin-v1", keyMessage = "mcp-operation-execution-origin-key-epoch-v1"
|
||||
@@ -142,7 +142,7 @@ export async function insertMcpOperationAdmission(client: PoolClient, start: Mcp
|
||||
const admission = Object.freeze(Object.create(null)) as McpOperationAdmission
|
||||
admissions.set(admission, { operationId: id, planId, connectionId: principal.connectionId, nonce: randomUUID(), principal, grant,
|
||||
generation: row.generation, directory: anchor.directory, keyEpoch: anchor.keyEpoch, operationName: row.operation_id, operationVersion: row.operation_version, policyVersion: row.policy_version,
|
||||
evidenceProof: sha(row.evidence.sealed), payloadProof: sha(row.payload.sealed) })
|
||||
evidenceProof: sha(row.evidence.sealed), payloadProof: sha(row.payload.sealed), approvalSource: row.evidence.approvalSource })
|
||||
return { operation: inserted.rows[0], admission, originalGrant: grant }
|
||||
}
|
||||
function grantFrom(row: Record<string, unknown>, principal: McpPrincipal) {
|
||||
@@ -160,23 +160,25 @@ function source(entry: Entry, row: Record<string, unknown>, state: "pending" | "
|
||||
|| state === "running" && row.claim_nonce !== entry.nonce || row.plan_state !== "executing" || row.owner_id !== m.ownerId
|
||||
|| row.operation_id !== m.operationName || row.operation_version !== m.operationVersion || row.policy_version !== m.policyVersion
|
||||
|| row.generation !== m.generation || row.connection_generation !== m.generation || !!row.approved_by !== m.approved
|
||||
|| m.approvalSource && row.approved_by !== m.ownerId
|
||||
|| !(row.expires_at instanceof Date) || row.expires_at.getTime() < m.originalExpiresAt
|
||||
|| !row.evidence || typeof row.evidence !== "object" || !row.payload || typeof row.payload !== "object") deny()
|
||||
const evidence = row.evidence as { sealed?: unknown }, payload = row.payload as { sealed?: unknown }
|
||||
if (typeof evidence.sealed !== "string" || typeof payload.sealed !== "string" || sha(evidence.sealed) !== m.sealedEvidenceProof || sha(payload.sealed) !== m.sealedPayloadProof) deny()
|
||||
const evidence = row.evidence as { sealed?: unknown; approvalSource?: unknown }, payload = row.payload as { sealed?: unknown }
|
||||
if (typeof evidence.sealed !== "string" || typeof payload.sealed !== "string" || evidence.approvalSource !== m.approvalSource || sha(evidence.sealed) !== m.sealedEvidenceProof || sha(payload.sealed) !== m.sealedPayloadProof) deny()
|
||||
const action = findActionByOperation(m.operationName)
|
||||
if (action.operation.version !== m.operationVersion) deny()
|
||||
const grant = grantFrom(row, m.principal)
|
||||
if (grant.proof !== m.originalTemporaryGrantProof || grant.expiresAt !== m.originalTemporaryGrantExpiresAt || !Array.isArray(row.protections) || row.protections.length > 1000) deny()
|
||||
const policy = row.policy as Parameters<typeof evaluateMcpPolicy>[0]["policy"]
|
||||
const protections = row.protections as Parameters<typeof evaluateMcpPolicy>[0]["protections"]
|
||||
if (policy.version !== m.policyVersion || !(row.owner_valid_until instanceof Date) || row.owner_valid_until.getTime() < m.originalExpiresAt) deny()
|
||||
if (policy.version !== m.policyVersion || m.approvalSource && !hasUnattendedPlanAuthority(policy) || !(row.owner_valid_until instanceof Date) || row.owner_valid_until.getTime() < m.originalExpiresAt) deny()
|
||||
const approval = operationApprovalRequirement(policy, m.operationName, m.evidence.target, m.evidence.requiresHuman)
|
||||
const decision = evaluateMcpPolicy({ operationId: m.operationName, policy, ownerPermissions: row.owner_permissions as string[], target: m.evidence.target,
|
||||
objects: [...m.evidence.objects, ...(m.evidence.requiresHuman && m.evidence.target.scope !== "servers" ? [{ kind: "mcp-operation", id: m.operationName }] : [])],
|
||||
objects: [...m.evidence.objects, ...approval.objects],
|
||||
objectCount: changeMutationCost(m.evidence) + (m.evidence.recovery ? 1 : 0),
|
||||
protections: [...protections, ...(m.evidence.requiresHuman && m.evidence.target.scope === "servers" ? m.evidence.target.serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId, protection: "human-approval-required" as const })) : [])],
|
||||
protections: [...protections, ...approval.protections],
|
||||
usage: { callsInMinute: 0, mutationsInHour: 0, concurrentReads: Number(row.concurrent_reads) }, auditAvailable: true, temporaryGrant: grant.value })
|
||||
if (decision.decision === "denied" || decision.decision === "approval-required" && !m.approved) deny()
|
||||
if (decision.decision === "denied" || decision.decision === "approval-required" && (!m.approved || m.approvalSource)) deny()
|
||||
}
|
||||
/** Two current joined snapshots around real principal/grant checks. Nothing is
|
||||
* cached across calls; the final SQL validates exact source + policy + grant. */
|
||||
@@ -268,13 +270,14 @@ export async function withMcpOperationExecutionOrigin(admission: unknown, work:
|
||||
if (row.operation_state !== "pending") { interruptOwn = false; return }
|
||||
if (!row.evidence?.sealed || !row.payload?.sealed || row.connection_generation !== generation || row.generation !== generation
|
||||
|| row.plan_id !== admitted.planId || generation !== admitted.generation || row.operation_id !== admitted.operationName || row.operation_version !== admitted.operationVersion
|
||||
|| row.policy_version !== admitted.policyVersion || sha(row.evidence.sealed) !== admitted.evidenceProof || sha(row.payload.sealed) !== admitted.payloadProof) return deny()
|
||||
|| row.policy_version !== admitted.policyVersion || row.evidence.approvalSource !== admitted.approvalSource
|
||||
|| sha(row.evidence.sealed) !== admitted.evidenceProof || sha(row.payload.sealed) !== admitted.payloadProof) return deny()
|
||||
const evidence = JSON.parse(await decryptSecret(row.evidence.sealed)) as ChangeEvidence, privateInput = JSON.parse(await decryptSecret(row.payload.sealed)) as Record<string, unknown>
|
||||
if (!Array.isArray(evidence.steps) || !Array.isArray(evidence.objects) || !Array.isArray(evidence.snapshots) || !privateInput || typeof privateInput !== "object" || Array.isArray(privateInput)) return deny()
|
||||
const expiresAt = Math.min(originalGrant.admissionExpiresAt, row.expires_at.getTime(), Date.parse(capturedPrincipal.credentialExpiresAt), Date.parse(connection.policy.expiresAt), connection.owner_valid_until.getTime(), evidence.validUntil ? Date.parse(evidence.validUntil) : Infinity)
|
||||
const metadata = frozen({ operationId, planId: String(row.plan_id), connectionId: capturedPrincipal.connectionId, ownerId: connection.owner_id,
|
||||
operationName: String(row.operation_id), operationVersion: Number(row.operation_version), policyVersion: Number(row.policy_version), generation, phase: "forward" as const,
|
||||
approved: !!row.approved_by, principal: capturedPrincipal, sealedEvidenceProof: sha(row.evidence.sealed), sealedPayloadProof: sha(row.payload.sealed),
|
||||
approved: !!row.approved_by, ...(admitted.approvalSource && { approvalSource: admitted.approvalSource }), principal: capturedPrincipal, sealedEvidenceProof: sha(row.evidence.sealed), sealedPayloadProof: sha(row.payload.sealed),
|
||||
originalTemporaryGrantProof: originalGrant.proof, originalTemporaryGrantExpiresAt: originalGrant.expiresAt, originalExpiresAt: expiresAt,
|
||||
serverIds: [...new Set([...(evidence.target.scope === "servers" ? evidence.target.serverIds : []), ...evidence.objects.flatMap(object => object.serverId === undefined ? [] : [object.serverId])])].sort((a, b) => a - b),
|
||||
resources: structuredClone(evidence.objects), privateInput, evidence })
|
||||
|
||||
@@ -10,7 +10,7 @@ assert.equal(findMcpOperation(fullFlowSettings.operation.id)?.version, 3)
|
||||
assert.equal(fullFlowSettings.schema.safeParse({ settings: { collectorIp: "10.255.254.1", enabled: false, mapServiceMinSharePct: 0 } }).success, true)
|
||||
assert.equal(fullFlowSettings.schema.safeParse({ topN: 10 }).success, false)
|
||||
assert.equal(findAction("mm_netflow_host_keys_generate_plan").operation.id, "traffic.flow.settings.generate-keys")
|
||||
assert.equal(findAction("mm_ipsec_user_create_plan").operation.version, 3)
|
||||
assert.equal(findAction("mm_ipsec_user_create_plan").operation.version, 4)
|
||||
assert.equal(findAction("mm_ipsec_server_init_plan").operation.version, 5)
|
||||
// RouterOS 7.24 certificate bodies (no comment, sign/export by .id): earlier prepared plans need a new approval.
|
||||
assert.equal(findAction("mm_ipsec_existing_certificate_export_plan").operation.version, 2)
|
||||
|
||||
@@ -13,7 +13,7 @@ import { decryptSecretExisting, fingerprint, privateFingerprinterExisting, redac
|
||||
const uuid = z.string().uuid()
|
||||
const positive = z.number().int().positive().safe()
|
||||
const object = z.record(z.string(),z.unknown())
|
||||
const sealed = z.object({sealed:z.string().min(1).max(262144)}).strict()
|
||||
const sealed = z.object({sealed:z.string().min(1).max(262144),approvalSource:z.literal("connection-policy").optional()}).strict()
|
||||
const ids = z.array(positive).max(1000)
|
||||
const retention = z.number().int().min(1).max(365)
|
||||
const bool = z.boolean()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import assert from "node:assert/strict"
|
||||
import { test } from "node:test"
|
||||
import { mcpOperationDescriptorSchema, mcpPolicyPreviewRequestSchema, mcpPolicyDecisionSchema, mcpTemporaryGrantSchema, type McpPolicyPreviewRequest } from "@mmapp/contracts/mcp"
|
||||
import { MCP_SERVICE_EXPIRATION, mcpConnectionPolicySchema, mcpOperationDescriptorSchema, mcpPolicyPreviewRequestSchema, mcpPolicyDecisionSchema, mcpTemporaryGrantSchema, type McpPolicyPreviewRequest } from "@mmapp/contracts/mcp"
|
||||
import { evaluateMcpPolicy } from "./policy.js"
|
||||
import { findMcpOperation, listMcpOperations } from "./registry.js"
|
||||
|
||||
@@ -42,6 +42,19 @@ test("mode × dangerous policy matrix never grants autonomy implicitly", () => {
|
||||
}
|
||||
})
|
||||
|
||||
test("unattended plans require an explicit service policy and retain server protections", () => {
|
||||
const input = request()
|
||||
assert.equal(mcpConnectionPolicySchema.safeParse({ ...input.policy, allowUnattendedPlans: true }).success, false)
|
||||
input.policy = mcpConnectionPolicySchema.parse({ ...input.policy, authority: "service", expiresAt: MCP_SERVICE_EXPIRATION })
|
||||
assert.equal(evaluateMcpPolicy(input, now).decision, "approval-required")
|
||||
input.policy.allowUnattendedPlans = true
|
||||
assert.equal(evaluateMcpPolicy(input, now).decision, "allowed")
|
||||
input.protections = [{ kind: "server", id: "1", protection: "human-approval-required" }]
|
||||
assert.equal(evaluateMcpPolicy(input, now).decision, "approval-required")
|
||||
input.protections[0].protection = "deny"
|
||||
denied(input, "PROTECTED_OBJECT")
|
||||
})
|
||||
|
||||
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")
|
||||
|
||||
@@ -3,10 +3,22 @@ import {
|
||||
type McpPolicyDecision,
|
||||
type McpPolicyPreviewRequest,
|
||||
type McpPolicyReasonCode,
|
||||
type McpConnectionPolicy,
|
||||
} from "@mmapp/contracts/mcp"
|
||||
import { hasPermission } from "../../lib/permissions.js"
|
||||
import { findMcpOperation } from "./registry.js"
|
||||
|
||||
export function hasUnattendedPlanAuthority(policy: McpConnectionPolicy) {
|
||||
return policy.authority === "service" && policy.mode === "autonomous" && policy.allowUnattendedPlans === 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 }]
|
||||
const protectedObjects = target.scope === "servers" ? target.serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId })) : objects
|
||||
return { objects, protections: protectedObjects.map(object => ({ ...object, protection: "human-approval-required" })) }
|
||||
}
|
||||
|
||||
/** Pure policy simulation. Does not grant authority, reserve budgets or execute handlers. */
|
||||
export function evaluateMcpPolicy(payload: McpPolicyPreviewRequest, now = new Date()): McpPolicyDecision {
|
||||
const input = mcpPolicyPreviewRequestSchema.parse(payload)
|
||||
@@ -87,7 +99,7 @@ export function evaluateMcpPolicy(payload: McpPolicyPreviewRequest, now = new Da
|
||||
else approve("Защищённый объект требует подтверждения человеком")
|
||||
}
|
||||
if (policy.mode === "approval") approve("Режим подключения требует подтверждения человеком")
|
||||
if (operation.risk === "dangerous" && !(policy.dangerousPolicy === "explicit-allow" && policy.autonomousDangerousOperationIds.includes(operation.id))) {
|
||||
if (operation.risk === "dangerous" && !hasUnattendedPlanAuthority(policy) && !(policy.dangerousPolicy === "explicit-allow" && policy.autonomousDangerousOperationIds.includes(operation.id))) {
|
||||
approve("Опасная операция не разрешена для автономного выполнения")
|
||||
}
|
||||
return result()
|
||||
|
||||
@@ -23,8 +23,9 @@ assert.match(basename(dirname(resolve(process.env.MCP_STATE_DIR))), /^mm-mcp-tes
|
||||
const { pool, closePool } = await import("../../db/index.js")
|
||||
const { applySqlMigrations } = await import("../../db/migrate.js")
|
||||
const { MIGRATION_MANIFEST } = await import("../../db/migration-manifest.js")
|
||||
const { mcpConnectionCreateSchema, mcpTemporaryGrantSchema } = await import("@mmapp/contracts/mcp")
|
||||
const { createMcpConnection, authenticateMcpToken, getConnection, validatePrincipal, effectiveMcpConnectionSource } = await import("./store.js")
|
||||
const { MCP_SERVICE_EXPIRATION, mcpConnectionCreateSchema, mcpTemporaryGrantSchema } = await import("@mmapp/contracts/mcp")
|
||||
const { createMcpConnection, authenticateMcpToken, getConnection, validatePrincipal, effectiveMcpConnectionSource,
|
||||
updateMcpConnection, revalidateMcpOwner, rotateConnectionToken, setConnectionStatus, intersectGrant } = await import("./store.js")
|
||||
const { acceptPortalRevocation } = await import("./portal-revocation.js")
|
||||
const { connectProbeStorage } = await import("../../services/server-probe-storage-connection.js")
|
||||
const { principalSourceSql, principalSourceStatementName } = await import("./principal-security-sql.js")
|
||||
@@ -83,6 +84,67 @@ try {
|
||||
assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n, MIGRATION_MANIFEST.length)
|
||||
priorEnabled = Boolean((await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled)
|
||||
await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1")
|
||||
await check("explicit all-server selection stores current IDs and the empty list stays empty", async () => {
|
||||
const { actor, principal } = await fixture()
|
||||
const input = mcpConnectionCreateSchema.parse({ name: "All server scope", expiresAt: new Date(actor.expiresAt!).toISOString(),
|
||||
permissions: ["mm:servers:read"], scope: { serverIds: [], operationIds: ["servers.list"], allowGlobal: false } })
|
||||
const insertServer = async (name: string) => Number((await pool.query("INSERT INTO servers(name,host,enabled) VALUES($1,'127.0.0.1',false) RETURNING id", [name])).rows[0].id)
|
||||
const serverIds: number[] = []
|
||||
try {
|
||||
serverIds.push(await insertServer("Owned all-scope first"))
|
||||
const current = (await pool.query("SELECT id FROM servers ORDER BY id")).rows.map(row => Number(row.id))
|
||||
assert.equal(mcpConnectionCreateSchema.safeParse({ ...input, selectAllServers: true, scope: { ...input.scope, serverIds } }).success, false)
|
||||
const access = await createMcpConnection({ ...input, selectAllServers: true }, actor)
|
||||
connectionIds.push(access.connection.id)
|
||||
const allPrincipal = await authenticateMcpToken(access.token)
|
||||
serverIds.push(await insertServer("Owned all-scope second"))
|
||||
const granted = await validatePrincipal(allPrincipal)
|
||||
assert.deepEqual(granted.policy.scope.serverIds, current)
|
||||
assert.equal(granted.policy.scope.allowGlobal, false)
|
||||
assert.equal(Object.hasOwn(granted.policy, "selectAllServers"), false)
|
||||
const limited = intersectGrant(granted, { id: randomUUID(), startsAt: new Date().toISOString(), expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
scope: { serverIds: [serverIds[0]], operationIds: ["servers.list"], allowGlobal: false } })
|
||||
assert.deepEqual(limited.policy.scope.serverIds, [serverIds[0]])
|
||||
const updated = await updateMcpConnection(principal.connectionId, { ...input, selectAllServers: true }, actor)
|
||||
assert.deepEqual(updated.policy.scope.serverIds, [...current, serverIds[1]])
|
||||
const emptied = await updateMcpConnection(principal.connectionId, input, actor)
|
||||
assert.deepEqual(emptied.policy.scope.serverIds, [])
|
||||
} finally {
|
||||
await pool.query("DELETE FROM servers WHERE id=ANY($1::bigint[])", [serverIds])
|
||||
}
|
||||
})
|
||||
await check("service access survives portal expiry and preserves token rotation and revocation", async () => {
|
||||
const { actor, principal: personal } = await fixture()
|
||||
const payload = { authority: "service", name: "Service access", mode: "autonomous", expiresAt: null,
|
||||
permissions: ["mm:servers:read"], scope: { serverIds: [], operationIds: ["capabilities.read"], allowGlobal: false } }
|
||||
const policy = mcpConnectionCreateSchema.parse(payload)
|
||||
assert.equal(mcpConnectionCreateSchema.safeParse({ ...payload, dangerousPolicy: "explicit-allow" }).success, false)
|
||||
assert.equal(mcpConnectionCreateSchema.safeParse({ ...payload, authority: undefined }).success, false)
|
||||
await assert.rejects(createMcpConnection(policy, { ...actor, permissions: policy.permissions }), { code: "PERMISSION_DENIED" })
|
||||
await assert.rejects(updateMcpConnection(personal.connectionId, policy, actor), { code: "INVALID_CONFIGURATION" })
|
||||
const shortSession = { ...actor, expiresAt: Date.now() + 250 }
|
||||
const { connection, token } = await createMcpConnection(policy, shortSession)
|
||||
connectionIds.push(connection.id)
|
||||
await new Promise(resolve => setTimeout(resolve, Math.max(0, shortSession.expiresAt - Date.now() + 1)))
|
||||
const principal = await authenticateMcpToken(token)
|
||||
const current = await validatePrincipal(principal)
|
||||
assert.equal(current.policy.authority, "service")
|
||||
assert.equal(current.owner_valid_until.toISOString(), MCP_SERVICE_EXPIRATION)
|
||||
assert.equal(principal.credentialExpiresAt, MCP_SERVICE_EXPIRATION)
|
||||
assert.deepEqual(current.owner_permissions, policy.permissions)
|
||||
await assert.rejects(revalidateMcpOwner(connection.id, actor), { code: "INVALID_CONFIGURATION" })
|
||||
const edited = await updateMcpConnection(connection.id, { ...policy, name: "Edited service access" }, actor)
|
||||
assert.equal(edited.ownerValidUntil, MCP_SERVICE_EXPIRATION)
|
||||
const rotated = await rotateConnectionToken(connection.id, actor)
|
||||
await assert.rejects(authenticateMcpToken(token), { code: "UNAUTHORIZED" })
|
||||
const replacement = await authenticateMcpToken(rotated.token)
|
||||
await setConnectionStatus(connection.id, "paused", actor)
|
||||
await assert.rejects(validatePrincipal(replacement), { code: "UNAUTHORIZED" })
|
||||
await setConnectionStatus(connection.id, "active", actor)
|
||||
await validatePrincipal(replacement)
|
||||
await setConnectionStatus(connection.id, "revoked", actor)
|
||||
await assert.rejects(authenticateMcpToken(rotated.token), { code: "UNAUTHORIZED" })
|
||||
})
|
||||
await check("poll current policy tuple: real SQL bounds, clock, backend, scopes and fresh committed updates", async () => {
|
||||
const f = await fixture()
|
||||
console.log("Owned poll policy tuple SQL:", JSON.stringify(await checkOwnedPollPolicySource(pool, f.principal.connectionId, f.principal.credentialId)))
|
||||
@@ -132,12 +194,17 @@ try {
|
||||
})
|
||||
await check("actual HMAC portal revoke and private external cutoff interrupt source capture", async () => {
|
||||
const f = await fixture(), secret = randomBytes(32).toString("hex")
|
||||
const service = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Revocable service", authority: "service", expiresAt: null,
|
||||
mode: "autonomous", permissions: [], scope: { serverIds: [], operationIds: ["capabilities.read"] } }), f.actor)
|
||||
connectionIds.push(service.connection.id)
|
||||
await validatePrincipal(await authenticateMcpToken(service.token))
|
||||
await refusalBetween(f, async () => {
|
||||
const timestamp = String(Math.floor(Date.now() / 1000)), raw = Buffer.from(JSON.stringify({ eventId: randomUUID(), userId: f.actor.id, type: "sessions.revoked" }))
|
||||
const signature = "sha256=" + createHmac("sha256", secret).update(timestamp + ".").update(raw).digest("hex")
|
||||
assert.deepEqual(await acceptPortalRevocation(raw, timestamp, signature, secret), { ok: true, duplicate: false })
|
||||
}, "OWNER_CLAIMS_REVOKED")
|
||||
await assert.rejects(validatePrincipal(f.principal), { code: "OWNER_CLAIMS_REVOKED" })
|
||||
await assert.rejects(authenticateMcpToken(service.token), { code: "UNAUTHORIZED" })
|
||||
})
|
||||
await check("duplicate signed revocation repairs the external cutoff without moving recorded authority", async () => {
|
||||
const f = await fixture(), secret = randomBytes(32).toString("hex")
|
||||
|
||||
@@ -6,6 +6,7 @@ import ts from "typescript"
|
||||
import { z } from "zod"
|
||||
import { routeAiControllerBlockReason, routeAiProfileSchema, usesScopedRouteAiContext } from "@mmapp/contracts/mcp-routing"
|
||||
import { fingerprint, McpError } from "./security.js"
|
||||
import { hasUnattendedPlanAuthority } from "./policy.js"
|
||||
|
||||
type Handler = { prepare?: (input: Record<string, unknown>, context: unknown) => Promise<Record<string, unknown>>; execute?: (input: Record<string, unknown>) => Promise<unknown> }
|
||||
async function load(name: string, providers: Record<string, unknown>) {
|
||||
@@ -47,6 +48,7 @@ async function fixture() {
|
||||
"./path-proof-purpose.js": { COMMUNITY_PATH_MAX_AGE_MS: 90000 },
|
||||
"./routing-controller-fence.js": { saveControllerProfileCas: async () => { casWrites++; return { id: "saved", version: 1 } } },
|
||||
"./routing-reservation-store.js": { createRoutingReservationStore: () => { throw new Error("Overlap-only fixture must not read or mutate routing charges") } },
|
||||
"./policy.js": { hasUnattendedPlanAuthority },
|
||||
})
|
||||
const register = profileModule.registerRoutingProfileActions as (register: (...args: unknown[]) => void) => void
|
||||
register((...args) => operations.set(String(args[0]), args.at(-1) as Handler))
|
||||
|
||||
@@ -134,6 +134,15 @@ export async function resolveOspfInterfaceSelectors(serverId: number, selectors:
|
||||
return names
|
||||
}
|
||||
|
||||
function bgpAddressFamily(value: string): number {
|
||||
if (!value.includes("/")) return isIP(value)
|
||||
try {
|
||||
return isIP(canonicalBgpPolicyPrefix(value).split("/")[0])
|
||||
} catch {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
export async function addRoutingDependencies(serverId: number, resource: string, action: RosStep["action"], current: ConfigurationRow | undefined, fields: Record<string, string>, snapshots: RosSnapshot[], objects: ChangeEvidence["objects"]) {
|
||||
const read = async (id: string) => {
|
||||
const existing = snapshots.find(snapshot => snapshot.resource === id)
|
||||
@@ -227,7 +236,11 @@ export async function addRoutingDependencies(serverId: number, resource: string,
|
||||
}
|
||||
if (resource === "bgp_connection") {
|
||||
const local = fields["local.address"] ?? current?.["local.address"], remote = fields["remote.address"] ?? current?.["remote.address"]
|
||||
if (typeof local === "string" && typeof remote === "string" && isIP(local) !== isIP(remote)) throw new McpError("INVALID_INPUT", "Адреса BGP-соединения должны быть одного семейства", 400)
|
||||
if (typeof local === "string" && typeof remote === "string") {
|
||||
const localFamily = bgpAddressFamily(local), remoteFamily = bgpAddressFamily(remote)
|
||||
if (!localFamily || !remoteFamily) throw new McpError("INVALID_INPUT", "Некорректный адрес или префикс BGP-соединения", 400)
|
||||
if (localFamily !== remoteFamily) throw new McpError("INVALID_INPUT", "Адреса BGP-соединения должны быть одного семейства", 400)
|
||||
}
|
||||
}
|
||||
if (resource === "bgp_instance" && current && ["as", "router-id", "disabled"].some(key => fields[key] !== undefined && fields[key] !== current[key])) {
|
||||
for (const row of (await read("bgp_connection")).rows.filter(row => [current.name, current[".id"]].includes(row.instance))) objects.push({ kind: "bgp_connection", id: String(row[".id"]), serverId })
|
||||
|
||||
@@ -16,6 +16,7 @@ import type { ActionContext } from "./action-types.js"
|
||||
import { COMMUNITY_PATH_MAX_AGE_MS } from "./path-proof-purpose.js"
|
||||
import { controllerProfileCurrent, publishControllerState, saveControllerProfileCas, withControllerProfileLease } from "./routing-controller-fence.js"
|
||||
import { assertIdentifiedRoutingCompletion, createRoutingReservationStore } from "./routing-reservation-store.js"
|
||||
import { hasUnattendedPlanAuthority } from "./policy.js"
|
||||
|
||||
const targetIds = (profile: RouteAiProfile) => [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId])])]
|
||||
const profilePublicColumns = "id,version,config,state,enabled,grant_expires_at,updated_at"
|
||||
@@ -142,6 +143,7 @@ async function tickAdmittedRoutingProfiles() {
|
||||
next.reason = "Изменение конфигурации проверено; ожидаются новые измерения сети"
|
||||
} else if (plan.operation_id) next.reason = "Операция выполняется"
|
||||
else if (plan.expires_at.getTime() <= Date.now()) { await stop("План истёк без подтверждения"); return }
|
||||
else if (plan.state === "draft" && hasUnattendedPlanAuthority(connection.policy)) next.reason = "Ожидается публикация плана клиентом MCP"
|
||||
else if (["approved", "draft"].includes(plan.state)) {
|
||||
await current()
|
||||
await assertRoutingControllerBaseline(state.baseline,row.id,row.version,profile,row.grant_expires_at.toISOString(),{principal,connection})
|
||||
|
||||
@@ -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 } from "./policy.js"
|
||||
import { evaluateMcpPolicy, hasUnattendedPlanAuthority, 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"
|
||||
@@ -105,14 +105,11 @@ async function policyInput(client: Pick<PoolClient, "query">, connection: Connec
|
||||
SELECT COALESCE((SELECT used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='calls' AND bucket=date_trunc('minute',now())),0) AS calls,
|
||||
COALESCE((SELECT used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations' AND bucket=date_trunc('hour',now())),0) AS mutations,
|
||||
(SELECT count(*)::int FROM mcp_read_leases WHERE connection_id=$1 AND expires_at>now()) AS reads`, [connection.id])
|
||||
const approval = operationApprovalRequirement(connection.policy, operationId, target, evidence?.requiresHuman)
|
||||
return {
|
||||
operationId, policy: connection.policy, ownerPermissions: connection.owner_permissions, target,
|
||||
objects: [...(evidence?.objects ?? []), ...(evidence?.requiresHuman && target.scope !== "servers" ? [{ kind: "mcp-operation", id: operationId }] : [])], objectCount: evidence ? changeMutationCost(evidence) + (evidence.recovery ? 1 : 0) : 0,
|
||||
protections: [
|
||||
...protections.map(row => mcpProtectedObjectSchema.parse(row.object_ref)),
|
||||
...(evidence?.requiresHuman && target.scope === "servers" ? target.serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId, protection: "human-approval-required" as const })) : []),
|
||||
...(evidence?.requiresHuman && target.scope !== "servers" ? [{ kind: "mcp-operation", id: operationId, protection: "human-approval-required" as const }] : []),
|
||||
],
|
||||
objects: [...(evidence?.objects ?? []), ...approval.objects], objectCount: evidence ? changeMutationCost(evidence) + (evidence.recovery ? 1 : 0) : 0,
|
||||
protections: [...protections.map(row => mcpProtectedObjectSchema.parse(row.object_ref)), ...approval.protections],
|
||||
usage: { callsInMinute: Number(usage.calls), mutationsInHour: Number(usage.mutations), concurrentReads: Number(usage.reads) }, auditAvailable: true,
|
||||
}
|
||||
}
|
||||
@@ -209,7 +206,8 @@ async function invokeMcpToolInner(principal: McpPrincipal, toolName: string, raw
|
||||
const id = randomUUID(), expiresAt = preparedPlanExpiry(action.operation.id, action.operation.section, evidence)
|
||||
if (!expiresAt) throw new McpError("PLAN_EXPIRED", "Срок исходных данных плана истёк или недоступен; подготовьте новый план", 409)
|
||||
const generation = await accessGeneration()
|
||||
await client.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) VALUES($1,$2,$3,$4,$5,$6,$7::jsonb,$8::jsonb,$9,$10)", [id, connection.id, action.operation.id, action.operation.version, connection.policy.version, generation, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(input)) }), JSON.stringify({ sealed: await encryptSecret(JSON.stringify(evidence)) }), decision.decision === "approval-required" ? "awaiting-approval" : "draft", expiresAt])
|
||||
let sealedEvidence = await encryptSecret(JSON.stringify(evidence))
|
||||
await client.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) VALUES($1,$2,$3,$4,$5,$6,$7::jsonb,$8::jsonb,$9,$10)", [id, connection.id, action.operation.id, action.operation.version, connection.policy.version, generation, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(input)) }), JSON.stringify({ sealed: sealedEvidence }), decision.decision === "approval-required" ? "awaiting-approval" : "draft", expiresAt])
|
||||
const vrfTicket = await prepareRuntimeVrfTicket({ operationId: action.operation.id, operationVersion: action.operation.version, planId: id, connectionId: connection.id, ownerId: connection.owner_id, evidence, sql: client,
|
||||
assertAuthority: async () => {
|
||||
if (signal?.aborted || generation !== await accessGeneration() || expiresAt.getTime() <= Date.now()) throw new McpError("PLAN_EXPIRED", "Подготовка VRF отменена или устарела", 409)
|
||||
@@ -238,11 +236,14 @@ async function invokeMcpToolInner(principal: McpPrincipal, toolName: string, raw
|
||||
finalCheck.usage.callsInMinute = Math.max(0, finalCheck.usage.callsInMinute - 1)
|
||||
decision = evaluateMcpPolicy(finalCheck)
|
||||
if (decision.decision === "denied") throw new McpError(decision.reasons[0].code, decision.reasons.map(reason => reason.message).join("; "))
|
||||
await client.query("UPDATE mcp_plans SET evidence=$2::jsonb,state=$3 WHERE id=$1", [id, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(evidence)) }), decision.decision === "approval-required" ? "awaiting-approval" : "draft"])
|
||||
sealedEvidence = await encryptSecret(JSON.stringify(evidence))
|
||||
await client.query("UPDATE mcp_plans SET evidence=$2::jsonb,state=$3 WHERE id=$1", [id, JSON.stringify({ sealed: sealedEvidence }), decision.decision === "approval-required" ? "awaiting-approval" : "draft"])
|
||||
}
|
||||
await audit(client, connection.owner_id, "plan.create", connection.id, { planId: id, operationId: action.operation.id, decision })
|
||||
assertActive()
|
||||
return { planId: id, expiresAt: expiresAt.toISOString(), state: decision.decision === "approval-required" ? "awaiting-approval" : "draft", decision, changes: redact(publicMcpChangeEvidence(action.operation.id, evidence)), ...(vrfTicket ? { vrfTicket } : {}), executionAvailable: true }
|
||||
const announcement = decision.decision === "allowed" && hasUnattendedPlanAuthority(connection.policy)
|
||||
? planAnnouncement(id, sealedEvidence) : undefined
|
||||
return { planId: id, expiresAt: expiresAt.toISOString(), state: decision.decision === "approval-required" ? "awaiting-approval" : "draft", decision, changes: redact(publicMcpChangeEvidence(action.operation.id, evidence)), ...(announcement && { operationId: action.operation.id, announcement }), ...(vrfTicket ? { vrfTicket } : {}), executionAvailable: true }
|
||||
})
|
||||
vrfPreparing = false
|
||||
} else throw new McpError("UNSUPPORTED_CAPABILITY", "Нет обработчика операции", 409)
|
||||
@@ -278,10 +279,19 @@ export async function loadPlan(id: string, client: Pick<PoolClient, "query"> = p
|
||||
if (!plan) throw new McpError("NOT_FOUND", "План не найден", 404)
|
||||
return plan
|
||||
}
|
||||
export async function publicPlan(plan: StoredPlan) {
|
||||
function planAnnouncement(planId: string, sealedEvidence: string) {
|
||||
return {
|
||||
digest: fingerprint({ planId, sealedEvidence }),
|
||||
instruction: "Сначала отправьте пользователю отдельное видимое сообщение в чате с ID плана, целями, конкретными изменениями и предупреждениями из changes. Затем вызовите mm_change_apply с announcedPlanDigest. Ответ пользователя для этого плана не требуется.",
|
||||
}
|
||||
}
|
||||
|
||||
export async function publicPlan(plan: StoredPlan, unattended = false) {
|
||||
const evidence = JSON.parse(await decryptSecret(plan.evidence.sealed)) as ChangeEvidence
|
||||
return { id: plan.id, connectionId: plan.connection_id, operationId: plan.operation_id, state: plan.state,
|
||||
expiresAt: plan.expires_at.toISOString(), createdAt: plan.created_at.toISOString(), approvedBy: plan.approved_by,
|
||||
expiresAt: plan.expires_at.toISOString(), createdAt: plan.created_at.toISOString(), approvedBy: plan.evidence.approvalSource ? null : plan.approved_by,
|
||||
...(plan.evidence.approvalSource && { approvalSource: plan.evidence.approvalSource, authorizedBy: plan.approved_by }),
|
||||
...(unattended && plan.state === "draft" && plan.expires_at.getTime() > Date.now() && { announcement: planAnnouncement(plan.id, plan.evidence.sealed) }),
|
||||
changes: redact(publicMcpChangeEvidence(plan.operation_id, evidence)), input: redact(JSON.parse(await decryptSecret(plan.payload.sealed))) }
|
||||
}
|
||||
export async function approvePlan(id: string, actor: AuthUser, approve: boolean) {
|
||||
@@ -378,10 +388,10 @@ export async function verifyStep(step: ChangeStep, result: unknown, before?: Ros
|
||||
if (!before) return false
|
||||
return verifyRosStepSnapshots(step.step, result, before, after ?? await readRosSnapshot(step.step.serverId, step.step.resource))
|
||||
}
|
||||
export async function applyPlan(principal: McpPrincipal, planId: string, idempotencyKey: string): Promise<StoredOperation> {
|
||||
return withMcpDatabaseRequest(() => admitPlan(principal, planId, idempotencyKey))
|
||||
export async function applyPlan(principal: McpPrincipal, planId: string, idempotencyKey: string, announcedPlanDigest?: string): Promise<StoredOperation> {
|
||||
return withMcpDatabaseRequest(() => admitPlan(principal, planId, idempotencyKey, announcedPlanDigest))
|
||||
}
|
||||
async function admitPlan(principal: McpPrincipal, planId: string, idempotencyKey: string): Promise<StoredOperation> {
|
||||
async function admitPlan(principal: McpPrincipal, planId: string, idempotencyKey: string, announcedPlanDigest?: string): Promise<StoredOperation> {
|
||||
const operationAdmissionStart = beginMcpOperationAdmission(principal)
|
||||
let reservedRouting = false
|
||||
let originalServiceGrant:OriginalServiceGrant|undefined
|
||||
@@ -408,6 +418,10 @@ async function admitPlan(principal: McpPrincipal, planId: string, idempotencyKey
|
||||
const action = findActionByOperation(plan.operation_id)
|
||||
if (plan.policy_version !== connection.policy.version || plan.operation_version !== action.operation.version || plan.generation !== await accessGeneration() || plan.expires_at.getTime() <= Date.now()) throw new McpError("PLAN_EXPIRED", "План или его разрешения устарели", 409)
|
||||
if (!["draft", "approved"].includes(plan.state)) throw new McpError("APPROVAL_REQUIRED", "План требует подтверждения или недоступен", 409)
|
||||
const delegated = plan.state === "draft" && hasUnattendedPlanAuthority(connection.policy)
|
||||
if (delegated && announcedPlanDigest !== planAnnouncement(plan.id, plan.evidence.sealed).digest) {
|
||||
throw new McpError("APPROVAL_REQUIRED", "Сначала опубликуйте точный план в чате и передайте его announcedPlanDigest", 409)
|
||||
}
|
||||
for (const change of evidence.steps) {
|
||||
if (change.kind === "ros" && change.step.resource === "ip_settings" && change.step.fields["rp-filter"] === "strict") {
|
||||
checkRpfPolicy(change.step.serverId, evidence.snapshots, "strict")
|
||||
@@ -415,7 +429,7 @@ async function admitPlan(principal: McpPrincipal, planId: string, idempotencyKey
|
||||
}
|
||||
let scopedPathExpiresAt: number | undefined
|
||||
if (plan.operation_id === "routing.community.switch" && requiresScopedRoutingOwnerApproval(evidence)) {
|
||||
if (plan.approved_by !== connection.owner_id) throw new McpError("APPROVAL_REQUIRED", "Переключение постоянного профиля подтверждает владелец подключения", 409)
|
||||
if (!delegated && plan.approved_by !== connection.owner_id) throw new McpError("APPROVAL_REQUIRED", "Переключение постоянного профиля подтверждает владелец подключения", 409)
|
||||
scopedPathExpiresAt = await readScopedRoutingPathAdmissionDeadline(client, evidence, connection.id)
|
||||
}
|
||||
if (evidence.validUntil && (!Number.isFinite(Date.parse(evidence.validUntil)) || Date.now() >= Date.parse(evidence.validUntil))) {
|
||||
@@ -449,18 +463,23 @@ async function admitPlan(principal: McpPrincipal, planId: string, idempotencyKey
|
||||
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 (delegated) {
|
||||
// Execution receipts use the delegating owner's ID. Object protections still require a human decision.
|
||||
plan.evidence.approvalSource = "connection-policy"
|
||||
await client.query("UPDATE mcp_plans SET state='executing',approved_by=$2,approved_at=now(),evidence=$3::jsonb WHERE id=$1", [planId, connection.owner_id, JSON.stringify(plan.evidence)])
|
||||
plan.approved_by = connection.owner_id
|
||||
} else await client.query("UPDATE mcp_plans SET state='executing' WHERE id=$1", [planId])
|
||||
const inserted = await insertMcpOperationAdmission(client, operationAdmissionStart, planId, idempotencyKey, originalServiceGrant)
|
||||
const row = inserted.operation, id = row.id
|
||||
operationAdmission = inserted.admission; originalServiceGrant = inserted.originalGrant
|
||||
if (plan.operation_id === "routing.community.switch" || plan.operation_id === "operations.recover") {
|
||||
bindRoutingChargeAdmission(client, connection.id, principal.credentialId, inserted.originalGrant)
|
||||
}
|
||||
await client.query("UPDATE mcp_plans SET state='executing' WHERE id=$1", [planId])
|
||||
if (reservedRouting) await reserveRouting(id)
|
||||
else if (plan.operation_id === "routing.community.switch") await admitPersistentRoutingForwardWithinOwner(client, id)
|
||||
else if (plan.operation_id === "operations.recover") await admitManualInverseWithinOwner(client, id, plan, evidence, principal,
|
||||
(profileId, current, observedAt, deadlineAt) => readHistoricalChargesWithinOwner(client, profileId, current, observedAt, deadlineAt))
|
||||
await audit(client, connection.owner_id, "operation.start", connection.id, { operationId: id, planId })
|
||||
await audit(client, connection.owner_id, "operation.start", connection.id, { operationId: id, planId, ...(delegated && { approvalSource: "connection-policy", announcedPlanDigest }) })
|
||||
return row
|
||||
}).catch(async error => { await retireMcpOperationAdmission(operationAdmission); throw error })
|
||||
const operation = admitted.value
|
||||
@@ -568,7 +587,7 @@ async function executeOperationWithLease(operationId: string, principal: McpPrin
|
||||
policy.usage.callsInMinute = 0; policy.usage.mutationsInHour = 0
|
||||
const decision = evaluateMcpPolicy(policy)
|
||||
if (decision.decision === "denied") throw new McpError(decision.reasons[0].code, decision.reasons.map(reason => reason.message).join("; "))
|
||||
if (decision.decision === "approval-required" && !plan.approved_by) throw new McpError("APPROVAL_REQUIRED", "Изменение теперь требует подтверждения", 409)
|
||||
if (decision.decision === "approval-required" && (!plan.approved_by || plan.evidence.approvalSource)) throw new McpError("APPROVAL_REQUIRED", "Изменение теперь требует подтверждения", 409)
|
||||
}
|
||||
if (policyLoan) await executeRuntimePollPolicyTransaction(policyLoan, checkPolicy)
|
||||
else await transaction(checkPolicy)
|
||||
@@ -681,7 +700,7 @@ async function executeOperationWithLease(operationId: string, principal: McpPrin
|
||||
}
|
||||
stepDispatches++
|
||||
} : undefined
|
||||
const result = await withVerifiedConfigurationChange({ actorId: connection.owner_id, connectionId: connection.id, serverIds, objects: evidence.objects, approved: !!plan.approved_by, serverWide: step.kind === "service", ...(serviceExecution?{serviceExecution}:{}), authorize: async () => { await (step.kind === "ros" ? authorizeCatalog() : authorize()) }, authorizeWrite: async () => { await (step.kind === "ros" ? catalogStage.fresh() : authorizeStage.fresh()) }, beforeDispatch },
|
||||
const result = await withVerifiedConfigurationChange({ actorId: connection.owner_id, connectionId: connection.id, serverIds, objects: evidence.objects, approved: !!plan.approved_by, approvalSource: plan.evidence.approvalSource, serverWide: step.kind === "service", ...(serviceExecution?{serviceExecution}:{}), authorize: async () => { await (step.kind === "ros" ? authorizeCatalog() : authorize()) }, authorizeWrite: async () => { await (step.kind === "ros" ? catalogStage.fresh() : authorizeStage.fresh()) }, beforeDispatch },
|
||||
async () => {
|
||||
vrfDiagnosticFallback?.executorEntered()
|
||||
if (step.kind === "ros") {
|
||||
@@ -830,7 +849,7 @@ async function performReservedRecovery(operation: StoredOperation, plan: StoredP
|
||||
input.temporaryGrant = await temporaryScope(client, connection, principal.credentialId)
|
||||
input.usage.callsInMinute = 0; input.usage.mutationsInHour = 0
|
||||
const decision = evaluateMcpPolicy(input)
|
||||
if (decision.decision === "denied" || decision.decision === "approval-required" && !plan.approved_by) throw new McpError("PERMISSION_DENIED", "Текущая защита или допуск запрещает восстановление")
|
||||
if (decision.decision === "denied" || decision.decision === "approval-required" && (!plan.approved_by || plan.evidence.approvalSource)) throw new McpError("PERMISSION_DENIED", "Текущая защита или допуск запрещает восстановление")
|
||||
})
|
||||
for (const channel of evidence.managementChannels ?? []) await verifyManagementChannel(channel.serverId, channel.channelId)
|
||||
if (!evidence.managementChannels?.length) throw new McpError("APPROVAL_REQUIRED", "Восстановление требует независимого канала")
|
||||
@@ -847,7 +866,7 @@ async function performReservedRecovery(operation: StoredOperation, plan: StoredP
|
||||
await audit(pool, connection.owner_id, "recovery.intent", connection.id, { operationId: operation.id, authorityProfileId: recovery.authorityProfileId })
|
||||
attempted = true
|
||||
const step = recovery.step
|
||||
const result = await withVerifiedConfigurationChange({ actorId: connection.owner_id, connectionId: connection.id, serverIds, objects: evidence.objects, approved: !!plan.approved_by, serverWide: true }, () => serviceExecute(step.service, step.input, { operationId: operation.id, phase: "recovery" }))
|
||||
const result = await withVerifiedConfigurationChange({ actorId: connection.owner_id, connectionId: connection.id, serverIds, objects: evidence.objects, approved: !!plan.approved_by, approvalSource: plan.evidence.approvalSource, serverWide: true }, () => serviceExecute(step.service, step.input, { operationId: operation.id, phase: "recovery" }))
|
||||
if (!await verifyStep(step, result)) throw new McpError("VERIFICATION_FAILED", "Проверка восстановления не прошла", 409)
|
||||
await pool.query("UPDATE mcp_recovery_intents SET state='verified',finished_at=now() WHERE operation_id=$1", [operation.id])
|
||||
await audit(pool, connection.owner_id, "recovery.verified", connection.id, { operationId: operation.id })
|
||||
|
||||
@@ -2,7 +2,7 @@ import { randomUUID } from "node:crypto"
|
||||
import type { PoolClient, QueryResultRow } from "pg"
|
||||
import { z } from "zod"
|
||||
import {
|
||||
mcpConnectionPolicySchema, mcpConnectionSchema, mcpSettingsSchema, mcpTemporaryGrantSchema,
|
||||
MCP_SERVICE_EXPIRATION, mcpConnectionPolicySchema, mcpConnectionSchema, mcpSettingsSchema, mcpTemporaryGrantSchema,
|
||||
type McpConnection, type McpConnectionCreate, type McpConnectionPolicy,
|
||||
} from "@mmapp/contracts/mcp"
|
||||
import { pool } from "../../db/index.js"
|
||||
@@ -19,6 +19,29 @@ export interface ConnectionRow extends QueryResultRow {
|
||||
owner_claims_issued_at: Date
|
||||
}
|
||||
function ownerValidUntil(actor: AuthUser) { return new Date(Math.min(actor.expiresAt ?? Date.now() + 900000, Date.now() + 86400000)) }
|
||||
function connectionExpiration(input: McpConnectionCreate, actor: AuthUser) {
|
||||
if (input.authority === "service" && input.expiresAt === null) {
|
||||
if (!hasPermission(actor.permissions, "mm:mcp:admin")) throw new McpError("PERMISSION_DENIED", "Сервисный доступ выдаёт администратор MCP", 403)
|
||||
return MCP_SERVICE_EXPIRATION
|
||||
}
|
||||
const expires = Date.parse(input.expiresAt ?? ""), now = Date.now()
|
||||
if (!input.expiresAt || !Number.isFinite(expires) || expires <= now || expires > now + 366 * 86400000)
|
||||
throw new McpError("INVALID_EXPIRATION", "Срок доступа должен быть от текущего времени до одного года", 400)
|
||||
return input.expiresAt
|
||||
}
|
||||
async function resolveConnectionScope(scope: McpConnectionCreate["scope"], selectAllServers: boolean, client: PoolClient) {
|
||||
if (selectAllServers) {
|
||||
if (scope.serverIds.length) throw new McpError("INVALID_CONFIGURATION", "Выберите все серверы или укажите конкретные ID", 400)
|
||||
const { rows } = await client.query<{ id: string }>("SELECT id FROM servers ORDER BY id LIMIT 1001")
|
||||
if (rows.length > 1000) throw new McpError("INVALID_CONFIGURATION", "Подключению можно выдать доступ не более чем к 1000 серверам", 400)
|
||||
return { ...scope, serverIds: rows.map(row => Number(row.id)) }
|
||||
}
|
||||
if (scope.serverIds.length) {
|
||||
const { rows } = await client.query("SELECT id FROM servers WHERE id=ANY($1::bigint[])", [scope.serverIds])
|
||||
if (rows.length !== scope.serverIds.length) throw new McpError("SERVER_DENIED", "Выбран неизвестный сервер", 400)
|
||||
}
|
||||
return scope
|
||||
}
|
||||
export interface McpPrincipal { connectionId: string; credentialId: string; credentialExpiresAt: string }
|
||||
export function intersectGrant(connection: ConnectionRow, grant?: z.infer<typeof mcpTemporaryGrantSchema>): ConnectionRow {
|
||||
if (!grant) return connection
|
||||
@@ -44,7 +67,7 @@ export async function effectiveMcpConnection(principal: McpPrincipal, client: Pi
|
||||
}
|
||||
export interface StoredPlan extends QueryResultRow {
|
||||
id: string; connection_id: string; operation_id: string; operation_version: number; policy_version: number;
|
||||
generation: string; payload: { sealed: string }; evidence: { sealed: string }; state: string;
|
||||
generation: string; payload: { sealed: string }; evidence: { sealed: string; approvalSource?: "connection-policy" }; state: string;
|
||||
expires_at: Date; approved_by: string | null; created_at: Date
|
||||
}
|
||||
export interface StoredOperation extends QueryResultRow {
|
||||
@@ -113,38 +136,41 @@ export function assertConnectionVisible(row: ConnectionRow, actor: AuthUser): vo
|
||||
if (row.owner_id !== actor.id && !hasPermission(actor.permissions, "mm:mcp:admin")) throw new McpError("NOT_FOUND", "Подключение не найдено", 404)
|
||||
}
|
||||
export async function createMcpConnection(input: McpConnectionCreate, actor: AuthUser) {
|
||||
const expires = Date.parse(input.expiresAt)
|
||||
if (expires <= Date.now() || expires > Date.now() + 366 * 86400000) throw new McpError("INVALID_EXPIRATION", "Срок доступа должен быть от текущего времени до одного года", 400)
|
||||
const expiresAt = connectionExpiration(input, actor)
|
||||
if (input.permissions.some(permission => !hasPermission(actor.permissions, permission))) throw new McpError("PERMISSION_DENIED", "Нельзя делегировать отсутствующие у владельца права")
|
||||
if (input.scope.serverIds.length) {
|
||||
const { rows } = await pool.query("SELECT id FROM servers WHERE id=ANY($1::bigint[])", [input.scope.serverIds])
|
||||
if (rows.length !== input.scope.serverIds.length) throw new McpError("SERVER_DENIED", "Выбран неизвестный сервер", 400)
|
||||
}
|
||||
const id = randomUUID(), generation = await accessGeneration()
|
||||
const { name, ...configuration } = input
|
||||
const policy = mcpConnectionPolicySchema.parse({ ...configuration, id, version: 1, status: "active" })
|
||||
const { name, scope: requestedScope, selectAllServers, ...configuration } = input
|
||||
const token = randomToken("bearer")
|
||||
return connectionWrite(async client => {
|
||||
await lockPortalOwner(client, actor.id)
|
||||
await assertPortalClaimsCurrent(actor, client)
|
||||
const { rows: [row] } = await client.query<ConnectionRow>("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,$2,$3,$4::jsonb,$5::jsonb,$6,$7,$8) RETURNING *", [id, name, actor.id, JSON.stringify(actor.permissions), JSON.stringify(policy), generation, ownerValidUntil(actor), new Date(actor.issuedAt ?? 0)])
|
||||
await client.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [randomUUID(), id, tokenDigest(token), input.expiresAt])
|
||||
const scope = await resolveConnectionScope(requestedScope, selectAllServers === true, client)
|
||||
const policy = mcpConnectionPolicySchema.parse({ ...configuration, scope, expiresAt, id, version: 1, status: "active" })
|
||||
const service = policy.authority === "service"
|
||||
const { rows: [row] } = await client.query<ConnectionRow>("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,$2,$3,$4::jsonb,$5::jsonb,$6,$7,$8) RETURNING *", [id, name, actor.id, JSON.stringify(service ? policy.permissions : actor.permissions), JSON.stringify(policy), generation, service ? expiresAt : ownerValidUntil(actor), new Date(actor.issuedAt ?? 0)])
|
||||
await client.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [randomUUID(), id, tokenDigest(token), expiresAt])
|
||||
await audit(client, actor.id, "connection.create", id, { name, policy })
|
||||
return { connection: publicConnection(row, generation), token }
|
||||
})
|
||||
}
|
||||
export async function updateMcpConnection(id: string, input: McpConnectionCreate, actor: AuthUser) {
|
||||
const expiresAt = connectionExpiration(input, actor)
|
||||
const service = input.authority === "service"
|
||||
if (input.permissions.some(permission => !hasPermission(actor.permissions, permission))) throw new McpError("PERMISSION_DENIED", "Нельзя делегировать отсутствующие права")
|
||||
if (Date.parse(input.expiresAt) > Date.now() + 366 * 86400000) throw new McpError("INVALID_EXPIRATION", "Срок доступа не может превышать год", 400)
|
||||
if (input.scope.serverIds.length && (await pool.query("SELECT id FROM servers WHERE id=ANY($1::bigint[])", [input.scope.serverIds])).rows.length !== input.scope.serverIds.length) throw new McpError("SERVER_DENIED", "Выбран неизвестный сервер", 400)
|
||||
return connectionWrite(async client => {
|
||||
if (service) {
|
||||
await lockPortalOwner(client, actor.id)
|
||||
await assertPortalClaimsCurrent(actor, client)
|
||||
}
|
||||
await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [id])
|
||||
const before = await getConnection(id, client)
|
||||
if (before.owner_id !== actor.id) throw new McpError("PERMISSION_DENIED", "Политику обновляет её владелец после подтверждения своих прав")
|
||||
if (Date.parse(input.expiresAt) <= Date.now()) throw new McpError("INVALID_EXPIRATION", "Истёк срок доступа", 400)
|
||||
const { name, ...configuration } = input
|
||||
const policy = mcpConnectionPolicySchema.parse({ ...configuration, id, version: before.policy.version + 1, status: before.policy.status })
|
||||
const { rows: [row] } = await client.query<ConnectionRow>("UPDATE mcp_connections SET name=$2,policy=$3::jsonb,owner_permissions=$4::jsonb,owner_valid_until=$5,updated_at=now() WHERE id=$1 RETURNING *", [id, name, JSON.stringify(policy), JSON.stringify(actor.permissions), ownerValidUntil(actor)])
|
||||
if (before.policy.authority !== input.authority) throw new McpError("INVALID_CONFIGURATION", "Для смены типа доступа создайте новое подключение", 400)
|
||||
if (Date.parse(expiresAt) <= Date.now()) throw new McpError("INVALID_EXPIRATION", "Истёк срок доступа", 400)
|
||||
const { name, scope: requestedScope, selectAllServers, ...configuration } = input
|
||||
const scope = await resolveConnectionScope(requestedScope, selectAllServers === true, client)
|
||||
const policy = mcpConnectionPolicySchema.parse({ ...configuration, scope, expiresAt, id, version: before.policy.version + 1, status: before.policy.status })
|
||||
const { rows: [row] } = await client.query<ConnectionRow>("UPDATE mcp_connections SET name=$2,policy=$3::jsonb,owner_permissions=$4::jsonb,owner_valid_until=$5,updated_at=now() WHERE id=$1 RETURNING *", [id, name, JSON.stringify(policy), JSON.stringify(service ? policy.permissions : actor.permissions), service ? before.owner_valid_until : ownerValidUntil(actor)])
|
||||
await client.query("UPDATE mcp_plans SET state='invalidated' WHERE connection_id=$1 AND state IN ('draft','awaiting-approval','approved')", [id])
|
||||
await audit(client, actor.id, "connection.policy.update", id, { before: before.policy, after: policy })
|
||||
return publicConnection(row, await accessGeneration())
|
||||
@@ -169,6 +195,7 @@ export async function revalidateMcpOwner(id: string, actor: AuthUser) {
|
||||
await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [id])
|
||||
const connection = await getConnection(id, client)
|
||||
if (connection.owner_id !== actor.id) throw new McpError("PERMISSION_DENIED", "Права подтверждает владелец подключения")
|
||||
if (connection.policy.authority === "service") throw new McpError("INVALID_CONFIGURATION", "Сервисное подключение не требует подтверждения прав владельца", 400)
|
||||
const policy = { ...connection.policy, version: connection.policy.version + 1 }
|
||||
const until = ownerValidUntil(actor)
|
||||
const { rows: [row] } = await client.query<ConnectionRow>("UPDATE mcp_connections SET owner_permissions=$2::jsonb,owner_valid_until=$3,policy=$4::jsonb,owner_claims_issued_at=$5,updated_at=now() WHERE id=$1 RETURNING *", [id, JSON.stringify(actor.permissions), until, JSON.stringify(policy), new Date(actor.issuedAt ?? 0)])
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -45,7 +45,11 @@ function makeMcpServerWithBindings(principal: McpPrincipal, bindings: ReturnType
|
||||
for (const action of bindings) {
|
||||
const descriptor = action.operation
|
||||
server.registerTool(descriptor.toolName, {
|
||||
title: descriptor.title, description: `${descriptor.title}. ${action.prepare ? "Подготавливает неизменяемый план; применение отдельным вызовом после проверки политики." : "Возвращает данные только из разрешённой области."}`,
|
||||
title: descriptor.title, description: `${descriptor.title}. ${action.prepare
|
||||
? "Подготавливает неизменяемый план. Если возвращён announcement, опубликуйте точные изменения отдельным сообщением в чате до вызова применения."
|
||||
: descriptor.id === "changes.apply"
|
||||
? "Если план содержит announcement, сначала отправьте точные изменения пользователю в чат, затем передайте announcedPlanDigest."
|
||||
: "Возвращает данные только из разрешённой области."}`,
|
||||
inputSchema: action.schema, annotations: descriptor.annotations,
|
||||
}, async (input, sdkContext) => {
|
||||
const execute = async () => {
|
||||
|
||||
@@ -111,6 +111,30 @@ try {
|
||||
} finally { pool.query = originalQuery; pool.connect = originalConnect }
|
||||
// Human JWT authentication stays outside the delegated MCP credential failure.
|
||||
const connectionSql = "SELECT * FROM mcp_connections WHERE id=$1"
|
||||
const serverChoicesUrl = "/api/mcp/server-choices"
|
||||
const scopedChoicesUrl = `${serverChoicesUrl}?connectionId=${createBody.connectionId}`
|
||||
const readMcp = { authorization: "Bearer " + await token(["mm:mcp:read"]) }
|
||||
let catalogOwnerId = "human-admin"
|
||||
const serverScopeQueries: unknown[] = []
|
||||
pool.query = (async (sql: string, values?: unknown[]) => {
|
||||
if (sql === connectionSql) return { rows: [{ id: createBody.connectionId, owner_id: catalogOwnerId, policy: payload.policy }] }
|
||||
assert.match(sql, /^SELECT id,name,host,enabled FROM servers/)
|
||||
serverScopeQueries.push(values?.[0])
|
||||
return { rows: [{ id: "1", name: "CHR test", host: "router.example.test", enabled: true, password: "synthetic-hidden" }] }
|
||||
}) as typeof pool.query
|
||||
try {
|
||||
assert.equal((await app.inject({ url: serverChoicesUrl })).statusCode, 401)
|
||||
assert.equal((await app.inject({ url: serverChoicesUrl, headers: readMcp })).statusCode, 403)
|
||||
const full = await app.inject({ url: serverChoicesUrl, headers: admin })
|
||||
const scoped = await app.inject({ url: scopedChoicesUrl, headers: readMcp })
|
||||
for (const response of [full, scoped]) {
|
||||
assert.equal(response.statusCode, 200)
|
||||
assert.deepEqual(response.json(), { servers: [{ id: 1, name: "CHR test", host: "router.example.test", enabled: true }] })
|
||||
}
|
||||
catalogOwnerId = "another-owner"
|
||||
assert.equal((await app.inject({ url: scopedChoicesUrl, headers: readMcp })).statusCode, 404)
|
||||
assert.deepEqual(serverScopeQueries, [null, [1]])
|
||||
} finally { pool.query = originalQuery }
|
||||
const credentialSql = `SELECT c.id,c.expires_at FROM mcp_credentials c WHERE c.connection_id=$1 AND c.kind='bearer'
|
||||
AND c.revoked_at IS NULL AND c.expires_at>now() AND NOT EXISTS(SELECT 1 FROM mcp_grants g WHERE g.payload->>'credentialId'=c.id::text)
|
||||
ORDER BY c.created_at DESC LIMIT 1`
|
||||
|
||||
@@ -160,6 +160,21 @@ export function createMcpRoutes(checkClaims: typeof assertPortalClaimsCurrent =
|
||||
})
|
||||
|
||||
app.get("/mcp/connections", async request => ({ connections: await listMcpConnections(actorOf(request.authUser)) }))
|
||||
app.get("/mcp/server-choices", async request => {
|
||||
const { connectionId } = z.object({ connectionId: uuid.optional() }).strict().parse(request.query)
|
||||
const actor = actorOf(request.authUser)
|
||||
let serverIds: number[] | null = null
|
||||
if (connectionId) {
|
||||
const connection = await getConnection(connectionId)
|
||||
assertConnectionVisible(connection, actor)
|
||||
serverIds = connection.policy.scope.serverIds
|
||||
} else if (!hasPermission(actor.permissions, "mm:mcp:admin")) {
|
||||
throw new McpError("PERMISSION_DENIED", "Полный каталог доступен администратору MCP", 403)
|
||||
}
|
||||
const { rows } = await pool.query<{ id: string; name: string; host: string; enabled: boolean }>(
|
||||
"SELECT id,name,host,enabled FROM servers WHERE $1::bigint[] IS NULL OR id=ANY($1::bigint[]) ORDER BY name,id", [serverIds])
|
||||
return { servers: rows.map(row => ({ id: Number(row.id), name: row.name, host: row.host, enabled: row.enabled })) }
|
||||
})
|
||||
app.post("/mcp/connections", async request => {
|
||||
const input = mcpConnectionCreateSchema.parse(request.body)
|
||||
const available = new Set(listActions().map(action => action.operation.id))
|
||||
|
||||
@@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto"
|
||||
import { channel } from "node:diagnostics_channel"
|
||||
import { createFlowReadResponseOwner, writeBoundedFlowSse, withFlowReadSourceWindow } from "../services/traffic-flow-read-budget.js"
|
||||
import { flowReadBudget, flowResponseReadLimits, FLOW_READ_RESERVATION_BYTES, FLOW_SSE_MAX_FRAME_BYTES } from "../services/traffic-flow-read-policy.js"
|
||||
import { readFlowWindow } from "../services/traffic-flow-read-window.js"
|
||||
import { updateTrafficFlowSettingsWithListener } from "../services/traffic-flow-settings-service.js"
|
||||
import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod"
|
||||
import { mcpDatabaseRequestSignal, withDatabaseApiRequest } from "../modules/mcp/database-request-lease.js"
|
||||
@@ -24,12 +25,10 @@ import { getFlowHostStatus, reconcileFlowHost, withFlowConfigurationLock } from
|
||||
import {
|
||||
ensureHostKeys,
|
||||
getTrafficFlowSettingsRow,
|
||||
toTrafficFlowSettingsDto,
|
||||
} from "../services/traffic-flow-settings.js"
|
||||
import {
|
||||
getFlowListenerState,
|
||||
drainExistingTrafficFlowWriters,
|
||||
getFlowRuntimeCounters,
|
||||
readTrafficFlowSettingsStatus,
|
||||
purgeTrafficFlowStore,
|
||||
listFlowTalkers,
|
||||
} from "../services/traffic-flow-ingest.js"
|
||||
@@ -98,7 +97,7 @@ function analyticsQuery(req: FastifyRequest) {
|
||||
|
||||
async function sendFlowTalkers(req: FastifyRequest, reply: FastifyReply) {
|
||||
const q = req.query as { range?: string }
|
||||
return reply.send(await listFlowTalkers(rangeToMinutes(q.range)))
|
||||
return reply.send(await readFlowWindow(rangeToMinutes(q.range), listFlowTalkers, mcpDatabaseRequestSignal()))
|
||||
}
|
||||
|
||||
function requestPublicHost(req: FastifyRequest): string {
|
||||
@@ -167,11 +166,7 @@ const trafficFlowRoutes: FastifyPluginAsyncZod = async (app) => {
|
||||
reconcile: () => withFlowConfigurationLock(reconcileFlowHost),
|
||||
}))
|
||||
await app.register(createTrafficFlowSettingsRoutes({
|
||||
read: async () => {
|
||||
const settings = await toTrafficFlowSettingsDto(getFlowListenerState())
|
||||
const { packetsReceived, lastExporterIp, lastError, lastDatagramAt,dataQuality } = await getFlowRuntimeCounters()
|
||||
return { ...settings, packetsReceived, lastExporterIp, lastError, lastDatagramAt,dataQuality }
|
||||
},
|
||||
read: readTrafficFlowSettingsStatus,
|
||||
updateWithListener: updateTrafficFlowSettingsWithListener,
|
||||
ensureKeys: ensureHostKeys,
|
||||
hasPrivateKey: async () => Boolean((await getTrafficFlowSettingsRow()).hostPrivateKey),
|
||||
@@ -200,20 +195,22 @@ const trafficFlowRoutes: FastifyPluginAsyncZod = async (app) => {
|
||||
|
||||
app.get("/traffic/flow/exporters", async (req, reply) => {
|
||||
const q = req.query as { range?: string }
|
||||
return reply.send(await listFlowExporters(rangeToMinutes(q.range)))
|
||||
return reply.send(await readFlowWindow(rangeToMinutes(q.range), listFlowExporters, mcpDatabaseRequestSignal()))
|
||||
})
|
||||
|
||||
app.get("/traffic/flow/clients", async (req, reply) => {
|
||||
const q = req.query as { range?: string }
|
||||
return reply.send(await listFlowClients(rangeToMinutes(q.range)))
|
||||
return reply.send(await readFlowWindow(rangeToMinutes(q.range), listFlowClients, mcpDatabaseRequestSignal()))
|
||||
})
|
||||
|
||||
app.get("/traffic/flow/analytics", async (req, reply) => {
|
||||
return reply.send(await buildFlowAnalytics(analyticsQuery(req)))
|
||||
const query = analyticsQuery(req)
|
||||
return reply.send(await readFlowWindow(query.minutes, minutes => buildFlowAnalytics({ ...query, minutes }), mcpDatabaseRequestSignal()))
|
||||
})
|
||||
|
||||
app.get("/traffic/flow/map-hops", async (req, reply) => {
|
||||
return reply.send(await buildFlowMapHops(analyticsQuery(req)))
|
||||
const query = analyticsQuery(req)
|
||||
return reply.send(await readFlowWindow(query.minutes, minutes => buildFlowMapHops({ ...query, minutes }), mcpDatabaseRequestSignal()))
|
||||
})
|
||||
|
||||
app.get("/traffic/flow/monthly", async (req, reply) => {
|
||||
|
||||
@@ -17,7 +17,8 @@ import { readTrafficLiveSample, TrafficLiveSourceChanged } from "../services/tra
|
||||
import { MikrotikClient, MikrotikError } from "../services/mikrotik.js"
|
||||
import { getEnabledServerById } from "../services/wireguard-live.js"
|
||||
import { readTrafficCollectionContext } from "../services/traffic-collection-state.js"
|
||||
import { readServerTrafficHistory, readServerTrafficInterfaces } from "../services/traffic-history-read.js"
|
||||
import { readServerTrafficHistory, readServerTrafficHistoryWithSeries, readServerTrafficInterfaces } from "../services/traffic-history-read.js"
|
||||
import { mergeBuiltTraffic } from "../services/traffic-rate.js"
|
||||
import {
|
||||
buildBoundInterfaceTraffic,
|
||||
buildUserTrafficList,
|
||||
@@ -93,8 +94,15 @@ const trafficRoutes: FastifyPluginAsyncZod = async (app) => {
|
||||
const rangeEndMs = Date.now(), rangeStartMs = rangeEndMs - rangeToMinutes(q.range) * 60000
|
||||
const allServers = await db.select().from(servers).where(eq(servers.enabled, true))
|
||||
const context = await readTrafficCollectionContext(allServers.map(server => server.id))
|
||||
const data = await Promise.all(allServers.map(server => readServerTrafficHistory(server, rangeStartMs, rangeEndMs, undefined, {}, context)))
|
||||
return reply.send({ servers: data })
|
||||
const data = await Promise.all(allServers.map(server => readServerTrafficHistoryWithSeries(server, rangeStartMs, rangeEndMs, undefined, {}, context)))
|
||||
const aggregate = mergeBuiltTraffic(data.map(item => item.series))
|
||||
return reply.send({
|
||||
servers: data.map(item => item.history),
|
||||
peaks: {
|
||||
rx: aggregate.observations?.rxPeak ? aggregate.rxPeak : null,
|
||||
tx: aggregate.observations?.txPeak ? aggregate.txPeak : null,
|
||||
},
|
||||
})
|
||||
})
|
||||
app.get("/traffic/servers/:id/live", async (req, reply) => {
|
||||
const p = req.params as { id?: string | number }
|
||||
|
||||
@@ -22,7 +22,7 @@ export interface VerifiedMcpServiceMetadata {
|
||||
}
|
||||
// `authorize` guards reads inside a stage and may reuse a recent proof; `authorizeWrite`, when given, is a
|
||||
// fresh check before every native or application write (each write is a stage boundary).
|
||||
interface VerifiedChange { active: boolean; actorId: string; connectionId: string; approved: boolean; serverIds: number[]; objects: McpObjectRef[]; serverWide: boolean; authorize?: () => Promise<void>; authorizeWrite?: () => Promise<void>; beforeDispatch?: () => Promise<void>; serviceExecution?:VerifiedMcpServiceMetadata }
|
||||
interface VerifiedChange { active: boolean; actorId: string; connectionId: string; approved: boolean; approvalSource?: "connection-policy" | null; serverIds: number[]; objects: McpObjectRef[]; serverWide: boolean; authorize?: () => Promise<void>; authorizeWrite?: () => Promise<void>; beforeDispatch?: () => Promise<void>; serviceExecution?:VerifiedMcpServiceMetadata }
|
||||
const verified = new AsyncLocalStorage<VerifiedChange>()
|
||||
declare const serviceExecutionBrand:unique symbol
|
||||
export type VerifiedMcpServiceExecution=Readonly<{[serviceExecutionBrand]:true}>
|
||||
@@ -136,7 +136,7 @@ export async function assertDatabaseRestoreAllowed() {
|
||||
const inherited = verified.getStore(), context = inherited?.active ? inherited : undefined
|
||||
const { rows } = await pool.query("SELECT object_ref FROM mcp_protections")
|
||||
for (const row of rows) {
|
||||
if (row.object_ref.protection === "deny" || !context?.approved) {
|
||||
if (row.object_ref.protection === "deny" || !context?.approved || context.approvalSource) {
|
||||
const code = row.object_ref.protection === "deny" ? "PROTECTED_OBJECT" : "APPROVAL_REQUIRED"
|
||||
await audit(pool, context?.actorId ?? actor.getStore() ?? "system", "configuration.denied", context?.connectionId ?? null, { domain: "database.restore", code })
|
||||
throw new McpError(code, "Полное восстановление затрагивает защищённый объект", 409)
|
||||
@@ -165,7 +165,7 @@ export async function executeGuardedConfigurationWrite<T>(serverId: number, meth
|
||||
// typed plan instead of guessing that their writes miss a protected object.
|
||||
const matches = !context || context.serverWide || protection.kind === "server" || context.objects.some(object => object.serverId === serverId && object.kind === protection.kind && object.id === protection.id)
|
||||
if (!matches) continue
|
||||
if (protection.protection === "deny" || !context?.approved) {
|
||||
if (protection.protection === "deny" || !context?.approved || context.approvalSource) {
|
||||
const code = protection.protection === "deny" ? "PROTECTED_OBJECT" : "APPROVAL_REQUIRED"
|
||||
await auditConfiguration(actorId, "configuration.denied", context?.connectionId ?? null, { serverId, method, path, code })
|
||||
throw new McpError(code, protection.protection === "deny" ? "Изменение заблокировано защитой объекта. Используйте план с проверкой зависимостей." : "Защищённый объект требует подтверждённого плана изменений.", 409)
|
||||
@@ -208,7 +208,7 @@ export async function executeGuardedApplicationWrite<T>(object: { kind: string;
|
||||
const { rows } = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=ANY($1::text[]) OR (object_ref->>'kind'='server' AND object_ref->>'id'=ANY($1::text[])) OR (object_ref->>'kind'=$2 AND object_ref->>'id'=$3) OR EXISTS(SELECT 1 FROM jsonb_array_elements($4::jsonb) AS target WHERE target->>'kind'=object_ref->>'kind' AND target->>'id'=object_ref->>'id')", [serverIds.map(String), object.kind, object.id, JSON.stringify(objects)])
|
||||
for (const row of rows) {
|
||||
const protection = row.object_ref as { protection: string }
|
||||
if (protection.protection === "deny" || !context?.approved) {
|
||||
if (protection.protection === "deny" || !context?.approved || context.approvalSource) {
|
||||
await auditConfiguration(actorId, "configuration.denied", context?.connectionId ?? null, { domain: "application", object, serverIds })
|
||||
throw new McpError(protection.protection === "deny" ? "PROTECTED_OBJECT" : "APPROVAL_REQUIRED", "Изменение приложения затрагивает защищённый объект; нужен проверенный план", 409)
|
||||
}
|
||||
|
||||
@@ -134,15 +134,7 @@ export function isReleasableIpsecCryptoRow(row: { name?: string; comment?: strin
|
||||
return isIpsecManagedComment(row.comment) || (!row.comment?.trim() && row.name === IPSEC_COMMON_NAME)
|
||||
}
|
||||
export const IPSEC_CA_COMMON_NAME = "MikrotikManager IPsec CA"
|
||||
/** RouterOS native id: `*<hex>` for configuration rows; RouterOS 7.24 `/file` rows use case-sensitive `**<base62>` ids
|
||||
* whose length follows the file path (stand probe 2026-11-21). */
|
||||
export function isRouterOsNativeId(value: unknown): value is string {
|
||||
return typeof value === "string" && value.length <= 256 && /^\*\*?[a-z0-9]+$/i.test(value)
|
||||
}
|
||||
/** Duplicate key: hex ids compare case-insensitively without leading zeros; `**` file ids compare exactly. */
|
||||
export function routerOsIdKey(value: string): string {
|
||||
return value.startsWith("**") ? value : value.toLowerCase().replace(/^\*0+(?=[a-z0-9])/, "*")
|
||||
}
|
||||
export { isRouterOsNativeId, routerOsIdKey } from "./routeros-native-paths.js"
|
||||
/** RouterOS 7.24 omits a peer `address` equal to the any-address from REST rows (stand probe 2026-11-21). */
|
||||
export const IPSEC_ANY_PEER_ADDRESS = "0.0.0.0/0"
|
||||
/** RouterOS 7.24 `/certificate` has no `comment` (PUT refuses it), so product certificates are recognised by their
|
||||
|
||||
@@ -96,12 +96,14 @@ export function normalizeIpsecExistingExportPassphrase(raw: string | undefined,
|
||||
}
|
||||
export function projectIpsecExistingCertificateExport(state: IpsecExistingCertificateExportState, filename: string, after: IpsecExistingCertificateExportState) {
|
||||
validateIpsecExistingCertificateExportState(after)
|
||||
const old = state.catalogs["/file"], candidates = after.catalogs["/file"].filter(row => row.name === filename && !old.some(prior => idKey(prior[".id"]) === idKey(row[".id"])))
|
||||
const old = state.catalogs["/file"], files = after.catalogs["/file"]
|
||||
const candidates = files.filter(row => row.name === filename && !old.some(prior => idKey(prior[".id"]) === idKey(row[".id"])))
|
||||
if (candidates.length !== 1) throw fail("Не подтверждён единственный receipt-owned файл экспорта", "VERIFICATION_FAILED")
|
||||
const row = candidates[0], size = Number(row.size)
|
||||
if (!Number.isSafeInteger(size) || size <= 0 || size > MAX_OWNED_FILE_BYTES) throw fail("Размер приватного файла вне предела точного /file/get (61438 байт)", "VERIFICATION_FAILED")
|
||||
const projected = structuredClone(state)
|
||||
projected.catalogs["/file"].push(structuredClone(row))
|
||||
// The new file can appear anywhere. The proof still checks the relative order and contents of every pre-existing row.
|
||||
projected.catalogs["/file"].splice(files.indexOf(row), 0, structuredClone(row))
|
||||
return { projected, file: { id: row[".id"], name: filename, size } }
|
||||
}
|
||||
export function projectIpsecExistingCertificateCleanup(state: IpsecExistingCertificateExportState, file: { id: string; name: string; size: number }) {
|
||||
|
||||
@@ -69,12 +69,13 @@ assert.throws(() => project(withCert, cert.commands[1], { observedRow: signed })
|
||||
const signedState = project(withCert, cert.commands[1], { observedRow: signed, ownedCertificateIds: new Set(["*new"]) })
|
||||
assert.deepEqual(signedState.catalogs["/certificate"].slice(0, 2), state.catalogs["/certificate"])
|
||||
assert.throws(() => project(withCert, cert.commands[1], { observedRow: { ...signed, opaque: "changed" }, ownedCertificateIds: new Set(["*new"]) }), /исходные поля/)
|
||||
const withFile = project(signedState, cert.commands[2], { assignedId: "*export", observedRow: { ".id": "*export", name: "mm-ipsec-fixture.p12", size: "256", type: "file" } })
|
||||
const withFile = project(signedState, cert.commands[2], { assignedId: "*export", observedRow: { ".id": "*export", name: "mm-ipsec-fixture.p12", size: "256", type: "file" }, exportIndex: 0 })
|
||||
assert.equal(withFile.catalogs["/file"][0][".id"], "*export")
|
||||
assert.deepEqual(project(withFile, cert.commands[3], { assignedId: "*export" }).catalogs["/file"], state.catalogs["/file"])
|
||||
assert.throws(() => project(state, psk.commands[0], { assignedId: "*l", observedRow: { ".id": "*l", ...psk.commands[0].fields } }), /ACK/)
|
||||
assert.throws(() => project(state, psk.commands[0], { assignedId: "*new", observedRow: { ".id": "*new", ...psk.commands[0].fields, "remote-id": "key-id:mallory" } }), /Поля/)
|
||||
const staticCert = plan(state, server, { serverId: 1, name: "Alice", staticIp: "192.0.2.7" }, "mm-ipsec-fixture")
|
||||
assert.deepEqual(staticCert.commands.map(row => row.action), ["create", "create", "sign", "create", "export", "cleanup"])
|
||||
assert.equal(staticCert.commands[3].fields["remote-certificate"], cert.certName)
|
||||
assert.equal(staticCert.commands[3].fields["match-by"], "certificate")
|
||||
assert.deepEqual(staticCert.commands.map(row => row.path), ["/certificate", "/certificate/sign", "/certificate/export-certificate", "/file", "/ip/ipsec/mode-config", "/ip/ipsec/identity"])
|
||||
assert.equal(staticCert.commands[5].fields["remote-certificate"], cert.certName)
|
||||
assert.equal(staticCert.commands[5].fields["match-by"], "certificate")
|
||||
console.log("ipsec-user-create-model: full PSK/certificate/static/export plans and raw conservation passed")
|
||||
@@ -45,6 +45,7 @@ export function planIpsecUserCreate(state: IpsecUserCreateState, server: ServerR
|
||||
if (mapClients(live).some(row => row.name === body.name.trim()) || (certName && live.certs.some(row => row.name === certName))) throw fail("Клиент уже существует")
|
||||
if (body.authMethod === "pre-shared-key" && live.identities.some(row => row.peer === peer.name && row["auth-method"] === "pre-shared-key" && row["remote-id"] === normalizeIpsecRemoteId(body.remoteId ?? body.name))) throw fail("Remote ID уже используется клиентом этого peer")
|
||||
const commands: IpsecUserCreateCommand[] = [], seeds: Array<{ path: string; row: Row }> = []
|
||||
let staticModeConfig: IpsecUserCreateCommand | undefined
|
||||
const seed = (path: string, row: unknown) => { if (row) seeds.push({ path, row: row as Row }) }
|
||||
seed("/ip/ipsec/peer", peer); seed("/ip/ipsec/mode-config", sharedMc)
|
||||
const sharedIdentity = selectSharedIke2Identity(live.identities, names)
|
||||
@@ -58,18 +59,18 @@ export function planIpsecUserCreate(state: IpsecUserCreateState, server: ServerR
|
||||
if (existing && (!isManagedIpsecModeConfig(existing) || c["/ip/ipsec/identity"].some(row => row["mode-config"] === existing.name || row["mode-config"]?.toLowerCase() === existing[".id"].toLowerCase()))) throw fail("Персональный mode-config уже используется другим клиентом; адрес чужого или общего объекта изменять нельзя")
|
||||
if (existing) for (const flag of ["dynamic", "builtin", "default"]) if (existing[flag] !== undefined && observedBoolean(existing[flag]) !== false) throw fail("Системный mode-config нельзя изменять", "PROTECTED_OBJECT")
|
||||
seed("/ip/ipsec/mode-config", existing)
|
||||
commands.push({ action: existing ? "update" : "create", path: "/ip/ipsec/mode-config", ...(existing ? { id: existing[".id"] } : {}), fields: { name: modeConfig, address: body.staticIp.trim(), responder: "yes", "address-prefix-length": "32" } })
|
||||
staticModeConfig = { action: existing ? "update" : "create", path: "/ip/ipsec/mode-config", ...(existing ? { id: existing[".id"] } : {}), fields: { name: modeConfig, address: body.staticIp.trim(), responder: "yes", "address-prefix-length": "32" } }
|
||||
}
|
||||
if (certName) {
|
||||
seed("/certificate", serverCert); seed("/certificate", c["/certificate"].find(row => row.name === caName))
|
||||
commands.push({ action: "create", path: "/certificate", fields: { name: certName, "common-name": body.name.trim(), "key-size": "2048", "key-usage": "digital-signature,key-encipherment,tls-client", "days-valid": String(body.daysValid ?? 1825) } })
|
||||
commands.push({ action: "sign", path: "/certificate/sign", name: certName, fields: { ca: caName! } })
|
||||
}
|
||||
if (body.authMethod !== "certificate" || body.staticIp || !sharedIdentity) commands.push({ action: "create", path: "/ip/ipsec/identity", fields: identityRosBody({ peerName: peer.name, modeConfig, comment: certName && !body.staticIp ? ipsecManagedComment("IKEv2 listener") : ipsecUserComment(body.name), authMethod: body.authMethod, certificate: certName ? serverCert!.name : undefined, remoteCertificate: body.staticIp ? certName : undefined, secret: body.psk, remoteId: body.authMethod === "pre-shared-key" ? body.remoteId ?? body.name : undefined, policyTemplateGroup: sharedIdentity?.["policy-template-group"] }) })
|
||||
if (certName) {
|
||||
if (c["/file"].some(row => row.name === `${exportPrefix}.p12`)) throw fail("Имя файла экспорта занято")
|
||||
commands.push({ action: "export", path: "/certificate/export-certificate", name: `${exportPrefix}.p12`, certificate: certName, fields: { type: "pkcs12", "file-name": exportPrefix, "export-passphrase": body.passphrase ?? "" } }, { action: "cleanup", path: "/file", name: `${exportPrefix}.p12`, fields: {} })
|
||||
}
|
||||
// Leave the client-specific IPsec settings untouched if the private export fails.
|
||||
if (staticModeConfig) commands.push(staticModeConfig)
|
||||
if (body.authMethod !== "certificate" || body.staticIp || !sharedIdentity) commands.push({ action: "create", path: "/ip/ipsec/identity", fields: identityRosBody({ peerName: peer.name, modeConfig, comment: certName && !body.staticIp ? ipsecManagedComment("IKEv2 listener") : ipsecUserComment(body.name), authMethod: body.authMethod, certificate: certName ? serverCert!.name : undefined, remoteCertificate: body.staticIp ? certName : undefined, secret: body.psk, remoteId: body.authMethod === "pre-shared-key" ? body.remoteId ?? body.name : undefined, policyTemplateGroup: sharedIdentity?.["policy-template-group"] }) })
|
||||
const kinds: Record<string, string> = { "/ip/ipsec/peer": "ipsec_peer", "/ip/ipsec/identity": "ipsec_identity", "/ip/ipsec/mode-config": "ipsec_mode_config", "/ip/pool": "ip_pool", "/certificate": "certificate", "/ip/ipsec/profile": "ipsec_profile", "/ip/ipsec/proposal": "ipsec_proposal", "/ip/ipsec/policy/group": "ipsec_policy_group", "/ip/ipsec/policy": "ipsec_policy" }
|
||||
const refs: Record<string, string> = { peer: "/ip/ipsec/peer", "mode-config": "/ip/ipsec/mode-config", "address-pool": "/ip/pool", certificate: "/certificate", "remote-certificate": "/certificate", ca: "/certificate", profile: "/ip/ipsec/profile", proposal: "/ip/ipsec/proposal", "policy-template-group": "/ip/ipsec/policy/group" }
|
||||
for (const command of commands) for (const [field, refPath] of Object.entries(refs)) {
|
||||
@@ -104,7 +105,7 @@ export function planIpsecUserCreate(state: IpsecUserCreateState, server: ServerR
|
||||
|
||||
/** Native defaults are admitted solely for the ACK-owned newly created row.
|
||||
* Signing can change only documented generated fields of that new certificate. */
|
||||
export function projectIpsecUserCreate(state: IpsecUserCreateState, command: IpsecUserCreateCommand, options: { assignedId?: string; observedRow?: Row; ownedCertificateIds?: Set<string> } = {}) {
|
||||
export function projectIpsecUserCreate(state: IpsecUserCreateState, command: IpsecUserCreateCommand, options: { assignedId?: string; observedRow?: Row; ownedCertificateIds?: Set<string>; exportIndex?: number } = {}) {
|
||||
const next = structuredClone(state)
|
||||
const path = command.action === "sign" ? "/certificate" : command.action === "export" || command.action === "cleanup" ? "/file" : command.path
|
||||
const rows = next.catalogs[path]
|
||||
@@ -126,11 +127,17 @@ export function projectIpsecUserCreate(state: IpsecUserCreateState, command: Ips
|
||||
rows[rows.indexOf(row)] = structuredClone(observed)
|
||||
} else {
|
||||
const row = options.observedRow, id = options.assignedId
|
||||
if (!row || !exactId(id) || row[".id"] !== id || rows.some(item => item[".id"].toLowerCase() === id.toLowerCase()) || Object.values(row).some(value => typeof value !== "string")) throw fail("Не подтверждён ACK нового объекта", "VERIFICATION_FAILED")
|
||||
if (!row || !exactId(id) || row[".id"] !== id || rows.some(item => routerOsIdKey(item[".id"]) === routerOsIdKey(id)) || Object.values(row).some(value => typeof value !== "string")) throw fail("Не подтверждён ACK нового объекта", "VERIFICATION_FAILED")
|
||||
for (const flag of ["dynamic", "builtin", "default"]) if (row[flag] !== undefined && observedBoolean(row[flag]) !== false) throw fail("Создан системный объект", "VERIFICATION_FAILED")
|
||||
const fields = command.action === "export" ? { name: command.name! } : command.fields
|
||||
for (const [key, value] of Object.entries(fields)) if (row[key] !== value && !(["disabled", "responder"].includes(key) && observedBoolean(row[key]) !== null && observedBoolean(row[key]) === observedBoolean(value))) throw fail("Поля нового объекта не подтверждены", "VERIFICATION_FAILED")
|
||||
rows.push(structuredClone(row))
|
||||
if (command.action === "export") {
|
||||
const index = options.exportIndex
|
||||
if (index === undefined || !Number.isInteger(index) || index < 0 || index > rows.length) throw fail("Не подтверждена позиция нового файла экспорта", "VERIFICATION_FAILED")
|
||||
rows.splice(index, 0, structuredClone(row))
|
||||
} else {
|
||||
rows.push(structuredClone(row))
|
||||
}
|
||||
}
|
||||
return next
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { randomBytes, randomUUID } from "node:crypto"
|
||||
import type { IpsecCertBundle } from "@mmapp/contracts/ipsec"
|
||||
import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js"
|
||||
import { McpError, fingerprint, privateFingerprinter } from "../modules/mcp/security.js"
|
||||
import { MAX_OWNED_FILE_BYTES, MikrotikClient } from "./mikrotik.js"
|
||||
import { MAX_OWNED_FILE_BYTES, MikrotikClient, MikrotikError } from "./mikrotik.js"
|
||||
import { readIpsecPeerDeleteState, ipsecPeerDeleteRows } from "./ipsec-peer-delete.js"
|
||||
import { ipsecUserUpdateValue } from "./ipsec-user-update.js"
|
||||
import { sameWireguardEndpoint } from "./wireguard-source.js"
|
||||
@@ -37,6 +37,15 @@ export async function createIpsecUserGuarded(server: ServerRow, raw: unknown, ex
|
||||
let completedCommands = 0, confirmedWrites = 0, bundle: IpsecCertBundle | undefined, artifactReceipt: Record<string, unknown> | undefined
|
||||
const stopped = (code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string) => new IpsecUserCreateExecutionError(code, message, confirmedWrites, completedCommands, plan.commands.length)
|
||||
for (const command of plan.commands) {
|
||||
const stage = {
|
||||
create: command.path === "/certificate" ? "создание сертификата" : command.path === "/ip/ipsec/mode-config" ? "назначение персонального IP" : "создание привязки IKEv2",
|
||||
update: "обновление персонального IP",
|
||||
sign: "подпись сертификата",
|
||||
export: "экспорт .p12",
|
||||
cleanup: "удаление временного .p12",
|
||||
}[command.action]
|
||||
const stoppedAtStage = (code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", detail: string) =>
|
||||
stopped(code, `Создание IKEv2 остановлено на этапе «${stage}». ${detail}`)
|
||||
let dispatched = false, response: unknown, assignedId: string | undefined
|
||||
const fence = async () => { const fresh = await readIpsecUserCreateState(server, source); if (fresh.proof !== state.proof) throw new McpError("STATE_CONFLICT", "IPsec изменился после durable intent; команда не отправлена", 409); await source(); dispatched = true }
|
||||
try {
|
||||
@@ -57,7 +66,8 @@ export async function createIpsecUserGuarded(server: ServerRow, raw: unknown, ex
|
||||
confirmedWrites++
|
||||
} catch (error) {
|
||||
if (!dispatched && !completedCommands && error instanceof McpError) throw error
|
||||
throw stopped(dispatched ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", "Создание IPsec остановлено; повтор native-команды запрещён, требуется сверка")
|
||||
const detail = !dispatched ? "Команда не отправлена." : error instanceof MikrotikError ? `RouterOS вернул HTTP ${error.statusCode}.` : "Ответ RouterOS не подтверждён."
|
||||
throw stoppedAtStage(dispatched ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", `${detail} Перед повтором требуется сверка.`)
|
||||
}
|
||||
let after: IpsecUserCreateState
|
||||
try {
|
||||
@@ -67,23 +77,27 @@ export async function createIpsecUserGuarded(server: ServerRow, raw: unknown, ex
|
||||
const deadline = Date.now() + 90000
|
||||
while (!after.catalogs["/certificate"].find(row => row.name === command.name)?.["invalid-after"] && Date.now() < deadline) { await new Promise(resolve => setTimeout(resolve, 1000)); after = await readIpsecUserCreateState(server, source) }
|
||||
}
|
||||
} catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Команда отправлена, но read-back недоступен; повтор запрещён") }
|
||||
} catch { throw stoppedAtStage("MUTATION_OUTCOME_UNKNOWN", "Команда отправлена. Не удалось перечитать состояние RouterOS.") }
|
||||
let exportIndex: number | undefined
|
||||
if (command.action === "export") {
|
||||
const files = after.catalogs["/file"]
|
||||
const candidates = files.filter(row => row.name === command.name && !state.catalogs["/file"].some(old => old[".id"] === row[".id"]))
|
||||
if (candidates.length !== 1) throw stoppedAtStage("VERIFICATION_FAILED", "В списке RouterOS не найден единственный новый файл с ожидаемым именем.")
|
||||
assignedId = candidates[0][".id"]
|
||||
exportIndex = files.indexOf(candidates[0])
|
||||
}
|
||||
try {
|
||||
const path = command.action === "sign" ? "/certificate" : command.action === "export" ? "/file" : command.path
|
||||
if (command.action === "create") {
|
||||
const ack = response as Record<string, unknown> | null
|
||||
if (typeof ack?.[".id"] !== "string") throw Error("missing create ACK")
|
||||
assignedId = ack[".id"]
|
||||
} else if (command.action === "export") {
|
||||
const candidates = after.catalogs["/file"].filter(row => row.name === command.name && !state.catalogs["/file"].some(old => old[".id"] === row[".id"]))
|
||||
if (candidates.length !== 1) throw Error("missing unique export file")
|
||||
assignedId = candidates[0][".id"]
|
||||
}
|
||||
const observedRow = after.catalogs[path].find(row => command.action === "sign" ? row.name === command.name : row[".id"] === assignedId)
|
||||
const projected = projectIpsecUserCreate(state, command, { assignedId, observedRow, ownedCertificateIds })
|
||||
const projected = projectIpsecUserCreate(state, command, { assignedId, observedRow, ownedCertificateIds, exportIndex })
|
||||
if (await ipsecUserCreateProof(projected) !== after.proof) throw Error("configuration mismatch")
|
||||
if (command.action === "create" && command.path === "/certificate") ownedCertificateIds.add(assignedId!)
|
||||
} catch { throw stopped("VERIFICATION_FAILED", "Не подтверждены команда и сохранность полного состояния IPsec; повтор запрещён") }
|
||||
} catch { throw stoppedAtStage("VERIFICATION_FAILED", "Проверка результата команды и состояния IPsec не пройдена.") }
|
||||
state = after; completedCommands++
|
||||
try {
|
||||
await source(); await options.checkpoint?.({ receiptId, commandCount: plan.commands.length, completedCommands, afterProof: state.proof })
|
||||
@@ -100,7 +114,7 @@ export async function createIpsecUserGuarded(server: ServerRow, raw: unknown, ex
|
||||
bundle = { user: body.name, serverEndpoint: plan.serverEndpoint, filename: file.name, contentB64: p12B64, mime: "application/x-pkcs12", passphrase, sswanFilename: `${plan.certName}.sswan`, sswanContent: buildSswanConfig({ name: `IKEv2 ${plan.serverEndpoint}`, serverEndpoint: plan.serverEndpoint, serverId: plan.serverEndpoint, p12B64 }), instructions: buildClientInstructions({ userName: body.name, serverEndpoint: plan.serverEndpoint, p12Filename: file.name, passphrase, dns: plan.dns }) }
|
||||
} finally { content.fill(0) }
|
||||
}
|
||||
} catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Команда проверена, но checkpoint или приватный артефакт не сохранён; повтор запрещён") }
|
||||
} catch { throw stoppedAtStage("MUTATION_OUTCOME_UNKNOWN", "Команда подтверждена. Не удалось прочитать приватный файл или сохранить квитанцию.") }
|
||||
}
|
||||
const reader = { get: async (path: string) => { await source(); if (!Object.hasOwn(state.catalogs, path)) throw new McpError("STATE_UNAVAILABLE", "Неизвестный каталог истории IPsec", 502); return structuredClone(state.catalogs[path]) } } as unknown as MikrotikClient
|
||||
let historyRecorded = false
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from "node:assert/strict"
|
||||
import { readFileSync } from "node:fs"
|
||||
import test from "node:test"
|
||||
import ts from "typescript"
|
||||
import { isRouterOsNativeId, routerOsIdKey } from "./routeros-native-paths.js"
|
||||
|
||||
// Execute the actual method with inert I/O and retained buffer references, so
|
||||
// cleanup is observable without intercepting global Buffer methods or networking.
|
||||
@@ -10,7 +11,7 @@ const begin = source.indexOf(" async downloadOwnedFile(")
|
||||
const end = source.indexOf(" /** Native text exports", begin)
|
||||
assert.ok(begin > 0 && end > begin)
|
||||
const script = ts.transpileModule(`
|
||||
const {rosRawRequest,extractRosContentsField,MAX_OWNED_FILE_BYTES,MikrotikError}=deps;
|
||||
const {rosRawRequest,extractRosContentsField,MAX_OWNED_FILE_BYTES,MikrotikError,isRouterOsNativeId,routerOsIdKey}=deps;
|
||||
class Reader { params={}; get=deps.get; ${source.slice(begin, end)} }
|
||||
return new Reader();
|
||||
`, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None } }).outputText
|
||||
@@ -24,6 +25,8 @@ for (const mode of ["success", "raw-revoked", "decode-error", "wrong-size", "met
|
||||
const reader = new Function("deps", script)({
|
||||
MAX_OWNED_FILE_BYTES: 61438,
|
||||
MikrotikError: class extends Error {},
|
||||
isRouterOsNativeId,
|
||||
routerOsIdKey,
|
||||
get: async () => [{ ".id": "*owned", name: "owned.p12", size: Buffer.byteLength(secret), revision: mode === "metadata-drift" && ++metadataCalls > 1 ? "changed" : "initial" }],
|
||||
rosRawRequest: async () => { reads++; return raw },
|
||||
extractRosContentsField: (value: Buffer) => { assert.equal(value, raw); if (mode === "decode-error") throw Error(secret); return decoded },
|
||||
|
||||
@@ -4,11 +4,20 @@ import { setTimeout as delay } from "node:timers/promises"
|
||||
import { MAX_OWNED_FILE_BYTES, MikrotikClient } from "./mikrotik.js"
|
||||
|
||||
const content = "synthetic-private-file-bytes", target = { id: "*owned", name: "owned.p12", size: Buffer.byteLength(content) }
|
||||
const currentTarget = { ...target, id: "**Qo24Mn6Ay8FA4" }
|
||||
let mode = "normal", allowed = true, requests: string[] = [], bodies: unknown[] = [], metadataCalls = 0, closed = false, sent = 0, controller: AbortController | undefined
|
||||
const router = createServer(async (req, res) => {
|
||||
const path = req.url ?? ""; requests.push(path); res.setHeader("content-type", "application/json")
|
||||
if (req.method === "GET") {
|
||||
metadataCalls++
|
||||
if (mode.startsWith("current-id")) {
|
||||
const id = mode === "current-id-replaced" && metadataCalls > 1 ? currentTarget.id.toLowerCase() : currentTarget.id
|
||||
res.end(JSON.stringify([
|
||||
{ ".id": id, name: target.name, size: String(target.size) },
|
||||
{ ".id": currentTarget.id.toUpperCase(), name: "other.p12", size: String(target.size) },
|
||||
]))
|
||||
return
|
||||
}
|
||||
const id = mode === "replace-before" || ["replace-after", "compat-replace"].includes(mode) && metadataCalls > 1 ? "*replacement" : target.id
|
||||
res.end(JSON.stringify([{ ".id": id, name: target.name, size: String(target.size), opaque: "stable" }])); return
|
||||
}
|
||||
@@ -39,6 +48,14 @@ try {
|
||||
assert.equal((await client.downloadOwnedFile(target, { authorize })).toString(), content)
|
||||
assert.deepEqual(requests, ["/rest/file", "/rest/file/get", "/rest/file"])
|
||||
assert.deepEqual(bodies, [{ ".id": target.id, "value-name": "contents" }])
|
||||
for (const mode of ["current-id", "current-id-replaced"]) {
|
||||
reset(mode)
|
||||
const request = client.downloadOwnedFile(currentTarget, { authorize })
|
||||
if (mode === "current-id") assert.equal((await request).toString(), content)
|
||||
else await assert.rejects(request, /ID\/name\/size/)
|
||||
assert.deepEqual(requests, ["/rest/file", "/rest/file/get", "/rest/file"])
|
||||
assert.deepEqual(bodies, [{ ".id": currentTarget.id, "value-name": "contents" }])
|
||||
}
|
||||
for (const compatibility of ["compat-400", "compat-404"]) {
|
||||
reset(compatibility)
|
||||
assert.equal((await client.downloadOwnedFile(target, { authorize })).toString(), content)
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { BandwidthNativeRequestBinder } from "./bandwidth-native-dispatch.j
|
||||
import { withConfigMutationLock, withApplicationMutationLock } from "./config-mutation-lock.js"
|
||||
import { executeGuardedConfigurationWrite } from "./config-mutation-policy.js"
|
||||
import { exportRouterConfiguration } from "./router-config-export.js"
|
||||
import { isRouterOsNativeId, routerOsIdKey } from "./routeros-native-paths.js"
|
||||
import type { Server } from "../db/schema.js"
|
||||
import { parseRosDataSizeBytes } from "./ros-metric-parse.js"
|
||||
import { readOspfTemplateFlags } from "./ospf-template-flags.js"
|
||||
@@ -1168,7 +1169,8 @@ export class MikrotikClient {
|
||||
* unsupported value-name parameter permits the older .proplist spelling.
|
||||
* Metadata and grants are checked around every request; never retarget by name. */
|
||||
async downloadOwnedFile(target: { id: string; name: string; size: number }, control: { authorize: () => Promise<unknown>; signal?: AbortSignal; timeoutMs?: number }): Promise<Buffer> {
|
||||
if (!/^\*[a-z0-9]+$/i.test(target.id) || !target.name || /[\x00-\x1f\x7f]/.test(target.name) || !Number.isSafeInteger(target.size) || target.size <= 0 || target.size > MAX_OWNED_FILE_BYTES) throw new Error("Private export must have an exact ID/name and size between 1 and 61438 bytes")
|
||||
if (!isRouterOsNativeId(target.id) || !target.name || /[\x00-\x1f\x7f]/.test(target.name) || !Number.isSafeInteger(target.size) || target.size <= 0 || target.size > MAX_OWNED_FILE_BYTES) throw new Error("Private export must have an exact ID/name and size between 1 and 61438 bytes")
|
||||
const idKey = routerOsIdKey(target.id)
|
||||
const duration = control.timeoutMs ?? 30000
|
||||
if (!Number.isSafeInteger(duration) || duration <= 0 || duration > 45000) throw new Error("Invalid private export deadline")
|
||||
const deadline = Date.now() + duration, timerSignal = AbortSignal.timeout(duration)
|
||||
@@ -1186,7 +1188,7 @@ export class MikrotikClient {
|
||||
const rows = await this.get<unknown>("/file", remaining(), signal, 1048576)
|
||||
await authorize()
|
||||
if (!Array.isArray(rows) || rows.length > 2000) throw new Error("Private export metadata unavailable")
|
||||
const matches = rows.filter(row => row && typeof row === "object" && (row[".id"]?.toLowerCase() === target.id.toLowerCase() || row.name === target.name))
|
||||
const matches = rows.filter(row => row && typeof row === "object" && ((isRouterOsNativeId(row[".id"]) && routerOsIdKey(row[".id"]) === idKey) || row.name === target.name))
|
||||
if (matches.length !== 1 || matches[0][".id"] !== target.id || matches[0].name !== target.name || Number(matches[0].size) !== target.size) throw new Error("Private export ID/name/size changed")
|
||||
return JSON.stringify(matches[0])
|
||||
}
|
||||
|
||||
@@ -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" }]) {
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
@@ -3,6 +3,15 @@ export function encodeRosId(rosId: string): string {
|
||||
return encodeURIComponent(id)
|
||||
}
|
||||
|
||||
/** RouterOS 7.24 file IDs use case-sensitive `**<base62>` instead of the legacy single asterisk. */
|
||||
export function isRouterOsNativeId(value: unknown): value is string {
|
||||
return typeof value === "string" && value.length <= 256 && /^\*\*?[a-z0-9]+$/i.test(value)
|
||||
}
|
||||
|
||||
export function routerOsIdKey(value: string): string {
|
||||
return value.startsWith("**") ? value : value.toLowerCase().replace(/^\*0+(?=[a-z0-9])/, "*")
|
||||
}
|
||||
|
||||
export const greDependencyPaths = ["/interface", "/interface/bridge/port", "/interface/vlan",
|
||||
"/interface/list/member", "/ip/vrf", "/ip/route", "/ipv6/route", "/routing/filter/rule",
|
||||
"/routing/ospf/interface-template", "/routing/bfd/configuration", "/routing/bgp/connection", "/routing/rule",
|
||||
|
||||
@@ -34,7 +34,8 @@ const admissionSchema = z.object({ receipt_id: uuid, identity_kind: z.literal("m
|
||||
const operationSchema = z.object({ id: uuid, plan_id: uuid, connection_id: uuid, state: z.literal("applied"), verification: z.literal("passed"),
|
||||
error_code: z.null(), result: z.unknown(), created_at: time, finished_at: time }).passthrough()
|
||||
const planSchema = z.object({ id: uuid, connection_id: uuid, operation_id: z.literal("servers.poll"), operation_version: z.literal(1),
|
||||
state: z.literal("completed"), created_at: time, approved_by: actor, approved_at: time, expires_at: time, payload: sealed, evidence: sealed }).passthrough()
|
||||
state: z.literal("completed"), created_at: time, approved_by: actor, approved_at: time, expires_at: time, payload: sealed,
|
||||
evidence: sealed.extend({ approvalSource: z.literal("connection-policy").optional() }) }).passthrough()
|
||||
const stepSchema = z.object({ operation_id: uuid, step_index: z.literal(0), server_id: z.null(), state: z.literal("verified"), intended_state: z.unknown(),
|
||||
result: z.unknown(), finished_at: time }).passthrough()
|
||||
const inputSchema = z.object({ serverId: sid, receiptId: uuid, inputDigest: hash, bindingCiphertext: z.string().min(1).max(2097152) }).strict()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { withPartitionWrite, isPartitionWriteOutcomeUnknown, PartitionWriteError, type PartitionWriteOptions } from "./partition-write-transaction.js"
|
||||
import { and, asc, desc, eq, gte, lte } from "drizzle-orm"
|
||||
import { and, asc, desc, eq, gte, inArray, lt, lte, sql } from "drizzle-orm"
|
||||
import { executeGuardedApplicationWrite } from "./config-mutation-policy.js"
|
||||
import { withApplicationCollectorLock } from "./config-mutation-lock.js"
|
||||
import { db, pool } from "../db/index.js"
|
||||
@@ -354,17 +354,37 @@ async function updateTrafficSettingsImpl(patch: {
|
||||
return await getSettingsRow()
|
||||
}
|
||||
|
||||
export interface TrafficHistoryReadOptions { untilIso?: string; maxRows?: number }
|
||||
export interface TrafficHistoryReadOptions {
|
||||
untilIso?: string
|
||||
maxRows?: number
|
||||
interfaceNames?: readonly string[]
|
||||
interfaceCountersOnly?: boolean
|
||||
precedingMaxIntervalMs?: number
|
||||
}
|
||||
export async function readServerSamplesInRange(serverId: number, sinceIso: string, options: TrafficHistoryReadOptions = {}) {
|
||||
if (options.maxRows !== undefined && (!Number.isSafeInteger(options.maxRows) || options.maxRows < 1 || options.maxRows > 100000)) throw new Error("Некорректный предел истории трафика")
|
||||
let query = db.select()
|
||||
.from(trafficSamples)
|
||||
.where(and(
|
||||
const scope = and(
|
||||
eq(trafficSamples.serverId, serverId),
|
||||
options.interfaceNames === undefined ? undefined : inArray(trafficSamples.interfaceName, options.interfaceNames),
|
||||
options.interfaceCountersOnly ? eq(trafficSamples.peerPublicKey, "") : undefined,
|
||||
)
|
||||
const inside = db.select().from(trafficSamples).where(and(scope, gte(trafficSamples.sampledAt, sinceIso),
|
||||
options.untilIso === undefined ? undefined : lte(trafficSamples.sampledAt, options.untilIso)))
|
||||
let query = inside.$dynamic()
|
||||
if (options.precedingMaxIntervalMs !== undefined) {
|
||||
const earliest = new Date(Date.parse(sinceIso) - options.precedingMaxIntervalMs).toISOString()
|
||||
const priorWave = db.select({ sampledAt: trafficSamples.sampledAt }).from(trafficSamples).where(and(
|
||||
eq(trafficSamples.serverId, serverId),
|
||||
gte(trafficSamples.sampledAt, sinceIso),
|
||||
options.untilIso === undefined ? undefined : lte(trafficSamples.sampledAt, options.untilIso),
|
||||
))
|
||||
.orderBy(asc(trafficSamples.sampledAt)).$dynamic()
|
||||
gte(trafficSamples.sampledAt, earliest),
|
||||
lt(trafficSamples.sampledAt, sinceIso),
|
||||
)).orderBy(desc(trafficSamples.sampledAt)).limit(1)
|
||||
// The predecessor is a single poll wave from the same SQL snapshot, even for a filtered interface.
|
||||
query = query.unionAll(db.select().from(trafficSamples).where(and(scope,
|
||||
gte(trafficSamples.sampledAt, earliest), lt(trafficSamples.sampledAt, sinceIso),
|
||||
sql`${trafficSamples.sampledAt} = (${priorWave})`,
|
||||
)))
|
||||
}
|
||||
query = query.orderBy(asc(trafficSamples.sampledAt))
|
||||
if (options.maxRows !== undefined) query = query.limit(options.maxRows + 1)
|
||||
const rows = await query
|
||||
if (options.maxRows !== undefined && rows.length > options.maxRows) throw new Error("История трафика превышает предел; выберите меньший диапазон")
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from "node:assert/strict"
|
||||
import { rememberServerIfaces, resetIfaceCacheForTests } from "./traffic-flow-ifindex.js"
|
||||
import {
|
||||
ingestParsedFlowsForServerForTests,
|
||||
getRingMbps,
|
||||
resetFlowRingsForTests,
|
||||
} from "./traffic-flow-ingest.js"
|
||||
import { buildFlowAnalytics, formatLiveSseFromBuilder, getFlowMonthly, listFlowClients, listFlowExporters, resetFlowAnalyticsCacheForTests } from "./traffic-flow-analytics.js"
|
||||
@@ -98,6 +99,9 @@ ingestParsedFlowsForServerForTests(serverId, [
|
||||
])
|
||||
|
||||
try {
|
||||
const ring = getRingMbps(serverId)
|
||||
assert.equal(ring.rxNow,0)
|
||||
assert.equal(ring.txNow,12800 * 8 / 2 / 1e6)
|
||||
const all = await buildFlowAnalytics({ minutes: 5, serverId })
|
||||
assert.equal(all.applications[0]?.label, "HTTPS")
|
||||
assert.ok(all.protocols.some((p) => p.label === "TCP"))
|
||||
@@ -119,6 +123,14 @@ try {
|
||||
|
||||
const other = await buildFlowAnalytics({ minutes: 5, serverId: 99 })
|
||||
assert.equal(other.conversations, 0)
|
||||
|
||||
ingestParsedFlowsForServerForTests(serverId, [
|
||||
{ src:"10.1.1.9", dst:"9.9.9.9", proto:6, srcPort:50001, dstPort:443, inIface:"2", bytes:1200, packets:2 },
|
||||
{ src:"8.8.4.4", dst:"9.9.9.9", proto:6, srcPort:50002, dstPort:443, inIface:"2", bytes:3400, packets:4 },
|
||||
])
|
||||
const mixed = await buildFlowAnalytics({ minutes:5, serverId })
|
||||
assert.equal(mixed.bytes, 12800 + 1200 + 3400)
|
||||
assert.equal(mixed.bpsNow, mixed.bytes * 8 / 300, "Unattributed transit remains in the total rate")
|
||||
} finally {
|
||||
resetFlowRingsForTests()
|
||||
resetIfaceCacheForTests()
|
||||
@@ -561,16 +573,21 @@ resetFlowRingsForTests()
|
||||
await dbQuery("DELETE FROM flow_buckets WHERE server_id=$1", [serverId])
|
||||
for (const minutesAgo of [2, 3, 4, 10]) {
|
||||
await dbQuery(`INSERT INTO flow_buckets (server_id,bucket_at,src,dst,in_iface,bytes,packets)
|
||||
VALUES ($1,date_trunc('minute',now())-($2::int*interval '1 minute'),'10.1.1.8','8.8.8.8','2',60000,10)`, [serverId, minutesAgo])
|
||||
VALUES ($1,date_trunc('minute',now())-($2::int*interval '1 minute'),'10.1.1.8','8.8.8.8','2',60000,10),
|
||||
($1,date_trunc('minute',now())-($2::int*interval '1 minute'),'8.8.8.8','10.1.1.8','11',240000,40)`, [serverId, minutesAgo])
|
||||
}
|
||||
for (const minutes of [5, 15]) for (const dedup of [true, false]) {
|
||||
resetFlowAnalyticsCacheForTests()
|
||||
const history = await buildFlowAnalytics({ minutes, serverId, dedup, excludeMesh:false, excludeOverlay:false })
|
||||
assert.equal(history.rxSeries.length, minutes)
|
||||
assert.equal(history.rxSeries.filter(v => v > 0).length, minutes === 5 ? 3 : 4)
|
||||
assert.deepEqual(history.rxSeries,history.txSeries.map(v => v * 4))
|
||||
const missing = await buildFlowAnalytics({ minutes, serverId, iface:"not-an-interface" })
|
||||
assert(missing.rxSeries.every(v => v === 0), "Historical chart respects interface filter")
|
||||
}
|
||||
const historicalCard = (await listFlowExporters(5)).exporters.find(s => s.id === String(serverId))!
|
||||
assert(historicalCard.txNow > 0)
|
||||
assert.equal(historicalCard.rxNow,historicalCard.txNow * 4)
|
||||
|
||||
} finally {
|
||||
for (const table of ["flow_daily_dims", "flow_hour_facts", "flow_daily_facts", "flow_buckets", "traffic_samples", "user_interface_bindings", "server_snapshots"]) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { flowReadDbAll } from "./traffic-flow-read-policy.js"
|
||||
import { readFlowWindow } from "./traffic-flow-read-window.js"
|
||||
import { isDeepStrictEqual } from "node:util"
|
||||
import { readFlowSourceLimits } from "./traffic-flow-source-limits.js"
|
||||
import { readFlowExporterInputs, readFlowClientInputs, flowCounter, flowCounterSum } from "./traffic-flow-catalog-read.js"
|
||||
@@ -21,14 +22,15 @@ import {
|
||||
listFlowRowsForWindow,
|
||||
type PendingFlowRow,
|
||||
} from "./traffic-flow-ingest.js"
|
||||
import { flowDataEpoch, MAX_PENDING, RING_OVERLAY } from "./traffic-flow-engine.js"
|
||||
import { ADMITTED_FLOW_DAY_DIM, flowDataEpoch, MAX_PENDING, OBSERVED_FLOW_IFACE_COVERAGE_DIM, OBSERVED_FLOW_IFACE_DIM } from "./traffic-flow-engine.js"
|
||||
import { resolveIfaceName } from "./traffic-flow-ifaces.js"
|
||||
import { factIfaceAliases } from "./traffic-flow-ifindex.js"
|
||||
import { getTrafficFlowSettingsRow } from "./traffic-flow-settings.js"
|
||||
import { applicationName, flowRowMatchesFilter } from "./traffic-flow-apps.js"
|
||||
import { dedupFlowRowsMaxBytes, flowTupleKey } from "./traffic-flow-dedup.js"
|
||||
import { collapseGreEspRows, dedupFlowRowsMaxBytes, flowTupleKey } from "./traffic-flow-dedup.js"
|
||||
import { enqueueRipeMisses } from "./traffic-flow-ripe.js"
|
||||
import { classifyFlowPlane, flowBps, shouldKeepPlane } from "./traffic-flow-planes.js"
|
||||
import { resolveInternetDest } from "./traffic-flow-dest.js"
|
||||
import { flowRateDirection, resolveInternetDest } from "./traffic-flow-dest.js"
|
||||
import { refreshFlowCatalogInBackground } from "./traffic-flow-classify.js"
|
||||
import {
|
||||
enGreIfaceNames,
|
||||
@@ -105,22 +107,29 @@ async function userIfaceAllow(userId: string): Promise<Map<number, Set<string>>
|
||||
return allow
|
||||
}
|
||||
|
||||
function seriesFromRows(rows: PendingFlowRow[], minutes: number): { rx: number[]; tx: number[] } {
|
||||
function seriesFromRows(rows: PendingFlowRow[], minutes: number, direction: (row: PendingFlowRow) => "rx" | "tx" | "both" | null, now = Date.now()) {
|
||||
const slots = Math.min(60, Math.max(5, minutes))
|
||||
const slotMs = (minutes * 60_000) / slots
|
||||
const start = Date.now() - minutes * 60_000
|
||||
const start = now - minutes * 60_000
|
||||
const rx = Array(slots).fill(0) as number[]
|
||||
const tx = Array(slots).fill(0) as number[]
|
||||
let undirectedBytes = 0
|
||||
for (const r of rows) {
|
||||
const t = Date.parse(r.bucketAt)
|
||||
if (!Number.isFinite(t)) continue
|
||||
const idx = Math.min(slots - 1, Math.max(0, Math.floor((t - start) / slotMs)))
|
||||
rx[idx] += r.bytes
|
||||
const side = direction(r)
|
||||
if (side === null) undirectedBytes += r.bytes
|
||||
if (side === "rx" || side === "both") rx[idx] += r.bytes
|
||||
if (side === "tx" || side === "both") tx[idx] += r.bytes
|
||||
}
|
||||
const slotSec = Math.max(1, slotMs / 1000)
|
||||
return {
|
||||
rx: rx.map((b) => bpsToMbps((b * 8) / slotSec)),
|
||||
tx,
|
||||
tx: tx.map((b) => bpsToMbps((b * 8) / slotSec)),
|
||||
rxNow: bpsToMbps(rx.reduce((a, b) => a + b, 0) * 8 / (minutes * 60)),
|
||||
txNow: bpsToMbps(tx.reduce((a, b) => a + b, 0) * 8 / (minutes * 60)),
|
||||
undirectedBytes,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,6 +192,35 @@ function analyticsQueryKey(q: FlowAnalyticsQuery): string {
|
||||
const ANALYTICS_CACHE_TTL_MS = 2000
|
||||
let analyticsCache: { key: string; at: number; dto: FlowAnalyticsDto } | null = null
|
||||
|
||||
async function readAdmittedFlowTotals(serverId: number, iface: string, minutes: number, now: number): Promise<FlowAnalyticsDto["admittedTotals"]> {
|
||||
const end = Math.floor(now / 60_000) * 60_000
|
||||
const from = new Date(end - minutes * 60_000).toISOString(), to = new Date(end).toISOString()
|
||||
type Totals = { bytes: number | string; packets: number | string; count: number; missing?: number }
|
||||
const params = [serverId, from, to]
|
||||
const [totals] = iface
|
||||
? await flowReadDbAll<Totals>(`
|
||||
SELECT COALESCE(SUM(observed.bytes), 0) AS bytes, COALESCE(SUM(observed.packets), 0) AS packets,
|
||||
COUNT(*)::int AS count,
|
||||
COUNT(*) FILTER (WHERE coverage.bytes IS DISTINCT FROM s.bytes OR coverage.packets IS DISTINCT FROM s.packets)::int AS missing
|
||||
FROM flow_minute_stats s
|
||||
LEFT JOIN flow_minute_dims coverage ON coverage.server_id = s.server_id AND coverage.bucket_at = s.bucket_at
|
||||
AND coverage.dim = ? AND coverage.key = 'all'
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT SUM(d.bytes) AS bytes, SUM(d.packets) AS packets
|
||||
FROM flow_minute_dims d
|
||||
WHERE d.server_id = s.server_id AND d.bucket_at = s.bucket_at AND d.dim = ? AND d.key = ANY(?::text[])
|
||||
) observed ON true
|
||||
WHERE s.server_id = ? AND s.bucket_at >= ? AND s.bucket_at < ?
|
||||
`, [OBSERVED_FLOW_IFACE_COVERAGE_DIM, OBSERVED_FLOW_IFACE_DIM, factIfaceAliases(iface, serverId), ...params])
|
||||
: await flowReadDbAll<Totals>(`
|
||||
SELECT COALESCE(SUM(bytes), 0) AS bytes, COALESCE(SUM(packets), 0) AS packets, COUNT(*)::int AS count
|
||||
FROM flow_minute_stats WHERE server_id = ? AND bucket_at >= ? AND bucket_at < ?
|
||||
`, params)
|
||||
// A minute can contain writes by both old and new collectors after an upgrade.
|
||||
if (!totals?.count || totals.missing) return undefined
|
||||
return { bytes: flowCounter(totals.bytes), packets: flowCounter(totals.packets), from, to, minutesWithData: totals.count }
|
||||
}
|
||||
|
||||
async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedFlowAnalyticsRead): Promise<FlowAnalyticsDto> {
|
||||
const authorize = scope?.authorize ?? (async () => {})
|
||||
await authorize()
|
||||
@@ -190,7 +228,8 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
|
||||
const top = Math.min(50, Math.max(10, settings.topN))
|
||||
const windowSec = Math.max(60, q.minutes * 60)
|
||||
await authorize()
|
||||
const raw = await listFlowRowsForWindow(q.minutes, q.serverId, { ...(scope ? { maxRows: scope.maxRows } : {}), untilIso: new Date().toISOString() })
|
||||
const now = Date.now()
|
||||
const raw = await listFlowRowsForWindow(q.minutes, q.serverId, { ...(scope ? { maxRows: scope.maxRows } : {}), untilIso: new Date(now).toISOString() })
|
||||
for (const row of raw) if (scope && (!scope.serverIds.includes(row.serverId) || !Number.isSafeInteger(row.bytes) || row.bytes < 0 || !Number.isSafeInteger(row.packets) || row.packets < 0)) throw new Error("Недостоверная строка аналитики NetFlow")
|
||||
await authorize()
|
||||
const catalog = await getServerCatalog(scope ? [q.serverId!] : undefined)
|
||||
@@ -207,6 +246,23 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
|
||||
|
||||
if (!scope) refreshFlowCatalogInBackground()
|
||||
|
||||
const matchesIngress = (row: PendingFlowRow) => flowRowMatchesFilter(row, resolveIfaceName(row.serverId, row.inIface).name, q, allow)
|
||||
const matchesEgress = (row: PendingFlowRow) => flowRowMatchesFilter({ serverId: row.serverId, inIface: row.outIface }, resolveIfaceName(row.serverId, row.outIface).name, q, allow)
|
||||
const serverDirection = (row: PendingFlowRow) => flowRateDirection(row.serverId, row, topo, catalog.byId.get(row.serverId)?.host)
|
||||
const interfaceDirection = (row: PendingFlowRow) => {
|
||||
const ingress = matchesIngress(row), egress = matchesEgress(row)
|
||||
return ingress === egress ? null : ingress ? "rx" : "tx"
|
||||
}
|
||||
const selected = ifaceFilter || allow ? raw.filter(row => matchesIngress(row) || matchesEgress(row)) : raw
|
||||
const { rows, espDirections } = collapseGreEspRows(selected, ifaceFilter ? interfaceDirection : serverDirection)
|
||||
const previousMinute = new Date(Math.floor(now / 60_000) * 60_000 - 60_000).toISOString()
|
||||
const ifacesForWire = new Set<string>()
|
||||
for (const row of selected) {
|
||||
ifacesForWire.add(resolveIfaceName(row.serverId, row.inIface).name)
|
||||
const egress = resolveIfaceName(row.serverId, row.outIface).name
|
||||
if (egress && egress !== "—") ifacesForWire.add(egress)
|
||||
}
|
||||
|
||||
const applications = new Map<string, { bytes: number; packets: number; label?: string }>()
|
||||
const protocols = new Map<string, { bytes: number; packets: number; label?: string }>()
|
||||
const sources = new Map<string, { bytes: number; packets: number; label?: string }>()
|
||||
@@ -226,15 +282,12 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
|
||||
const skipHeavy = Boolean(q.skipHeavy)
|
||||
let bytesPayload = 0
|
||||
let bytesOverlay = 0
|
||||
let recentOverlayBytes = 0
|
||||
let bytesMesh = 0
|
||||
const ifacesForWire = new Set<string>()
|
||||
|
||||
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
|
||||
ifacesForWire.add(resolved.name)
|
||||
if (outResolved.name && outResolved.name !== "—") ifacesForWire.add(outResolved.name)
|
||||
const plane = classifyFlowPlane({
|
||||
src: r.src,
|
||||
dst: r.dst,
|
||||
@@ -245,7 +298,10 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
|
||||
outIface: outResolved.name,
|
||||
}, topo.plane)
|
||||
if (plane === "payload") bytesPayload += r.bytes
|
||||
else if (plane === "overlay") bytesOverlay += r.bytes
|
||||
else if (plane === "overlay") {
|
||||
bytesOverlay += r.bytes
|
||||
if (r.bucketAt === previousMinute) recentOverlayBytes += r.bytes
|
||||
}
|
||||
else if (plane === "client_mesh") bytesMesh += r.bytes
|
||||
if (!shouldKeepPlane(plane, { excludeMesh, excludeOverlay })) continue
|
||||
matched.push(r)
|
||||
@@ -435,12 +491,25 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
|
||||
const topCategory = topLabel(categories)
|
||||
|
||||
const ringServer = q.serverId ?? (matched[0]?.serverId ?? 0)
|
||||
const ring = ringServer
|
||||
? getRingMbps(ringServer, ifaceFilter === "" ? "__all__" : (ifacesMap.get(ifaceFilter)?.index || ifaceFilter))
|
||||
: { rx: Array(60).fill(0) as number[], tx: Array(60).fill(0) as number[], rxNow: 0, txNow: 0 }
|
||||
const ring = ringServer && !ifaceFilter && !allow && excludeOverlay ? getRingMbps(ringServer) : null
|
||||
|
||||
// The live ring covers about one minute; historical charts must use the requested window.
|
||||
const fromBuckets = seriesFromRows(matched, q.minutes)
|
||||
// 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 => {
|
||||
if (ifaceFilter || allow) {
|
||||
const ingress = matchesIngress(row), egress = matchesEgress(row)
|
||||
if (ingress && egress) {
|
||||
// A repeated WAN interface in an overlay record does not prove two wire directions.
|
||||
if (ifaceFilter && (row.proto === 47 || row.proto === 50)) return espDirections.get(row) ?? serverDirection(row)
|
||||
return "both"
|
||||
}
|
||||
// At a client binding, ingress is the client's upload.
|
||||
return allow && !ifaceFilter ? (ingress ? "tx" : "rx") : (ingress ? "rx" : "tx")
|
||||
}
|
||||
return espDirections.get(row) ?? serverDirection(row)
|
||||
}, now)
|
||||
const rxSeries = fromBuckets.rx
|
||||
const txSeries = fromBuckets.tx
|
||||
|
||||
@@ -478,19 +547,27 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
|
||||
.sort((a, b) => b.bytes - a.bytes)
|
||||
.slice(0, top)
|
||||
|
||||
const overlayRing = ringServer
|
||||
? getRingMbps(ringServer, RING_OVERLAY)
|
||||
: { rxNow: 0, txNow: 0 }
|
||||
// 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 bpsNow = ring
|
||||
? (!fromBuckets.undirectedBytes && (ring.rxNow + ring.txNow) * 1_000_000) || (totalBytes * 8) / windowSec
|
||||
: (recentBytes * 8) / 60 || (totalBytes * 8) / windowSec
|
||||
const greNames = ringServer ? enGreIfaceNames(topo, ringServer, [...ifacesForWire]) : []
|
||||
await authorize()
|
||||
const wire = ringServer ? await latestWireBps(ringServer, greNames, Boolean(scope)) : { bps: 0, bytes: 0 }
|
||||
await authorize()
|
||||
const admittedTotals = q.serverId !== undefined && !q.userId
|
||||
? await readAdmittedFlowTotals(q.serverId, ifaceFilter, q.minutes, now)
|
||||
: undefined
|
||||
|
||||
if (scope && (!Number.isSafeInteger(totalBytes) || !Number.isSafeInteger(totalPackets) || !Number.isSafeInteger(bytesPayload) || !Number.isSafeInteger(bytesOverlay) || !Number.isSafeInteger(bytesMesh))) throw new Error("Переполнение счётчика аналитики NetFlow")
|
||||
await authorize()
|
||||
if (scope && (!isDeepStrictEqual(topo, await loadFlowTopology(scope.serverIds, authorize)) || (await readFlowSourceLimits()).topN !== settings.topN)) throw new Error("Топология или предел источника NetFlow изменились во время чтения")
|
||||
await authorize()
|
||||
return {
|
||||
bpsNow: (ring.rxNow + ring.txNow) * 1_000_000 || (totalBytes * 8) / windowSec,
|
||||
timeBasis: "collector-receipt",
|
||||
...(admittedTotals ? { admittedTotals } : {}),
|
||||
bpsNow,
|
||||
bytes: totalBytes,
|
||||
packets: totalPackets,
|
||||
conversations: conv.size,
|
||||
@@ -528,37 +605,39 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
|
||||
bytesOverlay,
|
||||
bytesMesh,
|
||||
bytesWire: wire.bytes,
|
||||
bpsOverlay: (overlayRing.rxNow + overlayRing.txNow) * 1_000_000 || (bytesOverlay * 8) / windowSec,
|
||||
bpsOverlay: (recentOverlayBytes * 8) / 60 || (bytesOverlay * 8) / windowSec,
|
||||
bpsWire: wire.bps,
|
||||
excludeMeshApplied: excludeMesh,
|
||||
excludeOverlayApplied: excludeOverlay,
|
||||
}
|
||||
}
|
||||
|
||||
function summarizeByServer(rows: PendingFlowRow[]) {
|
||||
const bytes = new Map<number, number>()
|
||||
const sessions = new Map<number, number>()
|
||||
function groupRowsByServer(rows: PendingFlowRow[]) {
|
||||
const byServer = new Map<number, PendingFlowRow[]>()
|
||||
for (const r of rows) {
|
||||
bytes.set(r.serverId, (bytes.get(r.serverId) ?? 0) + r.bytes)
|
||||
sessions.set(r.serverId, (sessions.get(r.serverId) ?? 0) + 1)
|
||||
const group = byServer.get(r.serverId)
|
||||
if (group) group.push(r)
|
||||
else byServer.set(r.serverId, [r])
|
||||
}
|
||||
return { bytes, sessions }
|
||||
return byServer
|
||||
}
|
||||
|
||||
export async function listFlowExporters(minutes: number): Promise<FlowExportersDto> {
|
||||
const runtime = await getFlowRuntimeCounters()
|
||||
const inputs = await readFlowExporterInputs(minutes)
|
||||
const rows = inputs.window.rows
|
||||
const { bytes, sessions } = summarizeByServer(rows)
|
||||
const ids = new Set<number>([...bytes.keys()])
|
||||
const byServer = groupRowsByServer(rows)
|
||||
const ids = new Set<number>(byServer.keys())
|
||||
for (const id of inputs.configuredIds) ids.add(id)
|
||||
const catalog = inputs.catalog
|
||||
const emptySeries = Array(60).fill(0) as number[]
|
||||
const topo = await loadFlowTopology()
|
||||
const exporters = catalog.list
|
||||
.filter((s) => ids.has(s.id))
|
||||
.map((s) => {
|
||||
const ring = getRingMbps(s.id, "__all__")
|
||||
const total = bytes.get(s.id) ?? 0
|
||||
const records = byServer.get(s.id) ?? []
|
||||
const rates = ring.rx.some(Boolean) || ring.tx.some(Boolean) ? ring
|
||||
: seriesFromRows(records, minutes, row => flowRateDirection(s.id, row, topo, s.host))
|
||||
return {
|
||||
id: String(s.id),
|
||||
name: s.name,
|
||||
@@ -566,12 +645,12 @@ export async function listFlowExporters(minutes: number): Promise<FlowExportersD
|
||||
site: s.site,
|
||||
country: s.country,
|
||||
status: snapshotStatus(s.id),
|
||||
rxNow: ring.rxNow || (total * 8) / Math.max(60, minutes * 60) / 1_000_000,
|
||||
txNow: ring.txNow,
|
||||
sessions: sessions.get(s.id) ?? 0,
|
||||
rxSeries: ring.rx.some((v) => v > 0) ? ring.rx : emptySeries,
|
||||
txSeries: ring.tx,
|
||||
bytes: total,
|
||||
rxNow: rates.rxNow,
|
||||
txNow: rates.txNow,
|
||||
sessions: records.length,
|
||||
rxSeries: rates.rx,
|
||||
txSeries: rates.tx,
|
||||
bytes: records.reduce((sum, row) => sum + row.bytes, 0),
|
||||
} satisfies FlowEntityCard
|
||||
})
|
||||
.sort((a, b) => b.rxNow - a.rxNow)
|
||||
@@ -598,8 +677,6 @@ export async function listFlowClients(minutes: number): Promise<{ clients: { id:
|
||||
byUser.set(b.userId, list)
|
||||
}
|
||||
const rows = inputs.window.rows
|
||||
const emptySeries = Array(60).fill(0) as number[]
|
||||
const windowSec = Math.max(60, minutes * 60)
|
||||
const clients: FlowEntityCard[] = []
|
||||
for (const u of users) {
|
||||
const userBinds = byUser.get(u.id) ?? []
|
||||
@@ -610,18 +687,15 @@ export async function listFlowClients(minutes: number): Promise<{ clients: { id:
|
||||
set.add(b.interfaceName)
|
||||
allow.set(b.serverId, set)
|
||||
}
|
||||
let total = 0
|
||||
let sessions = 0
|
||||
for (const r of rows) {
|
||||
const resolved = resolveIfaceName(r.serverId, r.inIface)
|
||||
const names = allow.get(r.serverId)
|
||||
if (!names) continue
|
||||
if (!names.has(resolved.name) && !names.has(r.inIface)) continue
|
||||
total += r.bytes
|
||||
sessions += 1
|
||||
const bound = (id: number, iface: string) => {
|
||||
const names = allow.get(id)
|
||||
return names?.has(iface) || names?.has(resolveIfaceName(id, iface).name)
|
||||
}
|
||||
const firstServer = userBinds[0]?.serverId
|
||||
const ring = firstServer ? getRingMbps(firstServer, "__all__") : { rx: emptySeries, tx: emptySeries, rxNow: 0, txNow: 0 }
|
||||
const matched = rows.filter(r => bound(r.serverId, r.inIface) || bound(r.serverId, r.outIface))
|
||||
const rates = seriesFromRows(matched, minutes, r => {
|
||||
if (!bound(r.serverId, r.inIface)) return "rx"
|
||||
return bound(r.serverId, r.outIface) ? "both" : "tx"
|
||||
})
|
||||
clients.push({
|
||||
id: u.id,
|
||||
name: u.login,
|
||||
@@ -629,12 +703,12 @@ export async function listFlowClients(minutes: number): Promise<{ clients: { id:
|
||||
site: `${userBinds.length} ifaces`,
|
||||
country: "UN",
|
||||
status: u.active ? "online" : "offline",
|
||||
rxNow: (total * 8) / windowSec / 1_000_000 || ring.rxNow,
|
||||
txNow: ring.txNow,
|
||||
sessions,
|
||||
rxSeries: ring.rx.some((v) => v > 0) ? ring.rx : emptySeries,
|
||||
txSeries: ring.tx,
|
||||
bytes: total,
|
||||
rxNow: rates.rxNow,
|
||||
txNow: rates.txNow,
|
||||
sessions: matched.length,
|
||||
rxSeries: rates.rx,
|
||||
txSeries: rates.tx,
|
||||
bytes: matched.reduce((sum, row) => sum + row.bytes, 0),
|
||||
})
|
||||
}
|
||||
clients.sort((a, b) => b.rxNow - a.rxNow)
|
||||
@@ -664,7 +738,7 @@ export async function safeBuildLiveFlowSample(q: Omit<FlowAnalyticsQuery, "minut
|
||||
}> {
|
||||
return formatLiveSseFromBuilder(async () => {
|
||||
const skipHeavy = isFlowAnalyticsDegraded()
|
||||
return await buildFlowAnalytics({ ...q, minutes: LIVE_ANALYTICS_MINUTES, skipHeavy }, scope)
|
||||
return readFlowWindow(LIVE_ANALYTICS_MINUTES, minutes => buildFlowAnalytics({ ...q, minutes, skipHeavy }, scope))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -704,8 +778,8 @@ export async function getFlowMonthly(month: string, serverId?: number | readonly
|
||||
if (!bounds) {
|
||||
throw Object.assign(new Error("Некорректный месяц отчёта"), { statusCode: 400 })
|
||||
}
|
||||
const params: Array<string | number> = [bounds.start, bounds.end]
|
||||
let where = "day >= ? AND day < ? AND dim IN ('country', 'service', 'asn')"
|
||||
const params: Array<string | number> = [bounds.start, bounds.end, ADMITTED_FLOW_DAY_DIM]
|
||||
let where = "day >= ? AND day < ? AND dim IN ('country', 'service', 'asn', ?)"
|
||||
if (Array.isArray(serverId)) {
|
||||
if (serverId.some(id => !Number.isSafeInteger(id) || id <= 0)) throw new Error("Некорректная область месячного NetFlow")
|
||||
if (serverId.length === 0) where += " AND false"
|
||||
@@ -715,38 +789,62 @@ export async function getFlowMonthly(month: string, serverId?: number | readonly
|
||||
params.push(serverId as number)
|
||||
}
|
||||
if (options && (!Number.isSafeInteger(options.maxKeys) || options.maxKeys < 1 || options.maxKeys > 10000)) throw new Error("Некорректный предел месячных dimension keys")
|
||||
if (options) params.push(options.maxKeys + 1)
|
||||
if (options) params.push(options.maxKeys + 2)
|
||||
const rows = await flowReadDbAll<{ dim: string; key: string; bytes: number; packets: number }>(`
|
||||
SELECT dim AS dim, key AS key, SUM(bytes) AS bytes, SUM(packets) AS packets
|
||||
FROM flow_daily_dims
|
||||
WHERE ${where}
|
||||
GROUP BY dim, key
|
||||
WITH scoped AS (
|
||||
SELECT server_id, day, dim, key, bytes, packets FROM flow_daily_dims WHERE ${where}
|
||||
), daily AS (
|
||||
SELECT server_id, day, dim, SUM(bytes) AS bytes, SUM(packets) AS packets
|
||||
FROM scoped GROUP BY server_id, day, dim
|
||||
), chosen AS (
|
||||
SELECT DISTINCT ON (server_id, day) dim, bytes, packets
|
||||
FROM daily
|
||||
ORDER BY server_id, day, bytes DESC, packets DESC, CASE dim WHEN 'service' THEN 1 WHEN 'asn' THEN 2 WHEN 'country' THEN 3 ELSE 0 END
|
||||
)
|
||||
SELECT dim, key, SUM(bytes) AS bytes, SUM(packets) AS packets
|
||||
FROM scoped WHERE dim IN ('country', 'service', 'asn') GROUP BY dim, key
|
||||
UNION ALL
|
||||
SELECT 'total', CASE WHEN BOOL_AND(dim NOT IN ('country','service','asn')) IS FALSE THEN 'legacy-dimensions' ELSE 'admitted-daily' END,
|
||||
COALESCE(SUM(bytes), 0), COALESCE(SUM(packets), 0)
|
||||
FROM chosen
|
||||
${options ? "LIMIT ?" : ""}
|
||||
`, params)
|
||||
if (options && rows.length > options.maxKeys) throw new Error("Месячный NetFlow превышает предел dimension keys")
|
||||
if (options && rows.length > options.maxKeys + 1) throw new Error("Месячный NetFlow превышает предел dimension keys")
|
||||
|
||||
const countries: Array<{ key: string; bytes: number; packets: number }> = []
|
||||
const services: Array<{ key: string; bytes: number; packets: number }> = []
|
||||
const asns: Array<{ key: string; bytes: number; packets: number }> = []
|
||||
let bytes = 0
|
||||
const total = rows.find(row => row.dim === "total")
|
||||
if (!total) throw new Error("Месячный итог NetFlow недоступен")
|
||||
const bytes = flowCounter(total.bytes)
|
||||
for (const row of rows) {
|
||||
const rec = { key: row.key, bytes: flowCounter(row.bytes), packets: flowCounter(row.packets) }
|
||||
if (row.dim === "country") {
|
||||
countries.push(rec)
|
||||
bytes = flowCounter(bytes + rec.bytes)
|
||||
} else if (row.dim === "service") services.push(rec)
|
||||
else if (row.dim === "asn") asns.push(rec)
|
||||
}
|
||||
const countryBytes = flowCounterSum(countries.map(row => row.bytes))
|
||||
if (countryBytes < bytes) {
|
||||
const packets = Math.max(0, flowCounter(total.packets) - flowCounterSum(countries.map(row => row.packets)))
|
||||
const unknown = countries.find(row => row.key === "unknown")
|
||||
if (unknown) {
|
||||
unknown.bytes = flowCounter(unknown.bytes + (bytes - countryBytes))
|
||||
unknown.packets = flowCounter(unknown.packets + packets)
|
||||
} else {
|
||||
countries.push({ key: "unknown", bytes: bytes - countryBytes, packets })
|
||||
}
|
||||
}
|
||||
const [year, monthIndex] = month.split("-").map(Number)
|
||||
const leap = year! % 4 === 0 && (year! % 100 !== 0 || year! % 400 === 0)
|
||||
const daysInMonth = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][monthIndex! - 1]!
|
||||
const windowSec = daysInMonth * 86_400
|
||||
const countryTotal = flowCounterSum(countries.map(row => row.bytes)) || bytes || 1
|
||||
return {
|
||||
month,
|
||||
bytes,
|
||||
countries: toBreakdown(countries, countryTotal, windowSec),
|
||||
services: toBreakdown(services, flowCounterSum(services.map(row => row.bytes)) || 1, windowSec),
|
||||
asns: toBreakdown(asns, flowCounterSum(asns.map(row => row.bytes)) || 1, windowSec),
|
||||
totalsSource: total.key === "legacy-dimensions" ? "legacy-dimensions" : "admitted-daily",
|
||||
countries: toBreakdown(countries, bytes, windowSec),
|
||||
services: toBreakdown(services, bytes, windowSec),
|
||||
asns: toBreakdown(asns, bytes, windowSec),
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getFlowReadResourceProfile } from "./traffic-flow-read-resource-profile.js"
|
||||
import type { QueryResultRow } from "pg"
|
||||
import { trackFlowReadSource } from "./traffic-flow-read-budget.js"
|
||||
import { FlowSqlSourceTooLarge } from "./traffic-flow-read-window.js"
|
||||
|
||||
export interface FlowBoundedQueryClient {
|
||||
query(sql: string, values: unknown[]): Promise<{ rows: Array<Record<string, unknown>>; fields?:Array<{name:string}> }>
|
||||
@@ -24,7 +25,8 @@ export async function readBoundedFlowSql<T extends QueryResultRow>(client: FlowB
|
||||
const preflight=await client.query(`WITH flow_read_source AS MATERIALIZED (${query}) SELECT count(*)::text AS "__flowRowCount",COALESCE(sum(octet_length(row_to_json(s)::text)),0)::text AS "__flowEncodedBytes" FROM flow_read_source s`,values)
|
||||
const n=String(preflight.rows[0]?.__flowRowCount),b=String(preflight.rows[0]?.__flowEncodedBytes)
|
||||
const measuredBytes=Number(b)+2+Math.max(Number(n)-1,0)
|
||||
if(!/^\d{1,15}$/.test(n)||!/^\d{1,15}$/.test(b)||Number(n)>limits.maxRows||measuredBytes>sourceByteLimit)throw Object.assign(Error("Источник NetFlow превышает допустимый объём; сократите область или окно"),{statusCode:409,code:"STATE_UNAVAILABLE"})
|
||||
if(!/^\d{1,15}$/.test(n)||!/^\d{1,15}$/.test(b))throw Error("Invalid bounded NetFlow source size")
|
||||
if(Number(n)>limits.maxRows||measuredBytes>sourceByteLimit)throw new FlowSqlSourceTooLarge()
|
||||
const reserved=admit(Number(n),measuredBytes)
|
||||
const countIndex = values.length + 1, bytesIndex = values.length + 2
|
||||
const result = await client.query(`WITH flow_read_source AS MATERIALIZED (${query}),
|
||||
@@ -36,7 +38,8 @@ export async function readBoundedFlowSql<T extends QueryResultRow>(client: FlowB
|
||||
if(result.fields && ["__flowRowCount","__flowEncodedBytes","__flowPresent"].some(name=>result.fields!.filter(field=>field.name===name).length!==1))throw Error("Reserved NetFlow source column collision")
|
||||
const row = result.rows[0]
|
||||
const count = String(row?.__flowRowCount), bytes = String(row?.__flowEncodedBytes)
|
||||
if (!row || !/^\d{1,15}$/.test(count) || !/^\d{1,15}$/.test(bytes) || Number(count) > reserved.maxRows || Number(bytes) + 2 + Math.max(Number(count)-1,0) > reserved.maxEncodedBytes) throw Object.assign(Error("Источник NetFlow превышает допустимый объём; сократите область или окно"), { statusCode: 409, code: "STATE_UNAVAILABLE" })
|
||||
if (!row || !/^\d{1,15}$/.test(count) || !/^\d{1,15}$/.test(bytes)) throw Error("Invalid bounded NetFlow source size")
|
||||
if (Number(count) > reserved.maxRows || Number(bytes) + 2 + Math.max(Number(count)-1,0) > reserved.maxEncodedBytes) throw new FlowSqlSourceTooLarge()
|
||||
if (Number(count) === 0) return []
|
||||
if (result.rows.length !== Number(count) || result.rows.some(item => item.__flowPresent !== true)) throw Error("Invalid bounded NetFlow SQL result")
|
||||
// Preserve the normal pg column parsers (not JSONB numeric/timestamp coercion).
|
||||
|
||||
@@ -18,9 +18,7 @@ export async function readFlowCatalogWindow(minutes: number, serverIds?: readonl
|
||||
if (selected?.length === 0) return { rows: [] as PendingFlowRow[], since, until, topRowsPerBucketLimit: null, topConversationsLimit: null }
|
||||
const settings = await getTrafficFlowSettingsRow()
|
||||
if (!Number.isSafeInteger(settings.topN) || settings.topN < 1 || settings.topN > 1000) throw fail("Не подтверждён topN источника NetFlow")
|
||||
const rows:PendingFlowRow[]=[]
|
||||
if(selected===undefined)rows.push(...await listStoredFlowRows(since,undefined,{untilIso:until}))
|
||||
else for(const serverId of selected)rows.push(...await listStoredFlowRows(since,serverId,{maxRows:Math.max(1,Math.floor(50000/selected.length)),untilIso:until}))
|
||||
const rows = await listStoredFlowRows(since, undefined, { serverIds: selected, maxRows: 49999, untilIso: until })
|
||||
for (const row of rows) { flowCounter(row.bytes); flowCounter(row.packets) }
|
||||
return { rows, since, until, topRowsPerBucketLimit: Math.max(20, settings.topN), topConversationsLimit: settings.topN }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import assert from "node:assert/strict"
|
||||
import {
|
||||
collapseGreEspRows,
|
||||
dedupFlowRowsAcrossExporters,
|
||||
dedupFlowRowsMaxBytes,
|
||||
flowConversationKey,
|
||||
@@ -36,4 +37,28 @@ assert.equal(across.length, 1)
|
||||
assert.equal(across[0]?.serverId, 7)
|
||||
assert.equal(across[0]?.bytes, 9_000)
|
||||
|
||||
const minute = "2026-11-22T06:26:00.000Z"
|
||||
const nextMinute = "2026-11-22T06:27:00.000Z"
|
||||
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" }
|
||||
const reverse = { ...esp, src: esp.dst, dst: esp.src }
|
||||
const foreign = { ...esp, serverId: 9 }
|
||||
const collapse = (rows: typeof gre[]) => collapseGreEspRows(rows, row => row.inIface === "2" ? "tx" : null)
|
||||
const layerPair = collapse([gre, esp, plain, reverse, foreign])
|
||||
assert.deepEqual(layerPair.rows, [esp, plain, reverse, foreign])
|
||||
assert.equal(layerPair.espDirections.get(esp), "tx")
|
||||
|
||||
const splitGre = [{ ...gre, bytes: 8_400, packets: 7 }, { ...gre, bucketAt: nextMinute, bytes: 3_600, packets: 3 }]
|
||||
const laterEsp = { ...esp, bucketAt: nextMinute }
|
||||
assert.deepEqual(collapse([...splitGre, laterEsp]).rows, [laterEsp])
|
||||
for (const unrelated of [
|
||||
{ ...esp, packets: esp.packets - 1 },
|
||||
{ ...esp, bytes: gre.bytes },
|
||||
{ ...esp, bytes: gre.bytes + gre.packets * 500 },
|
||||
{ ...esp, bucketAt: "2026-11-22T06:29:00.000Z" },
|
||||
{ ...esp, srcPort: 123 },
|
||||
]) assert.deepEqual(collapse([gre, unrelated]).rows, [gre, unrelated])
|
||||
assert.deepEqual(collapseGreEspRows([gre, esp], row => row.proto === 47 ? "rx" : "tx").rows, [gre, esp])
|
||||
|
||||
console.log("traffic-flow-dedup.test.ts: ok")
|
||||
@@ -1,3 +1,5 @@
|
||||
import { isUnspecifiedIp } from "./traffic-flow-ip.js"
|
||||
|
||||
export interface FlowTupleRow {
|
||||
serverId: number
|
||||
src: string
|
||||
@@ -68,3 +70,66 @@ export function dedupFlowRowsMaxBytes<T extends FlowTupleRow>(rows: T[]): T[] {
|
||||
}
|
||||
return [...byTuple.values()]
|
||||
}
|
||||
|
||||
type TimedFlowRow = FlowTupleRow & { bucketAt: string }
|
||||
type FlowDirection = "rx" | "tx"
|
||||
type OverlayRows<T> = { gre: T[]; esp: T[] }
|
||||
|
||||
function account(rows: readonly FlowTupleRow[]) {
|
||||
return rows.reduce((total, row) => ({ bytes: total.bytes + row.bytes, packets: total.packets + row.packets }), { bytes: 0, packets: 0 })
|
||||
}
|
||||
|
||||
/** GRE and its IPsec envelope can both be exported for the same packet sequence. */
|
||||
export function collapseGreEspRows<T extends TimedFlowRow>(rows: readonly T[], direction: (row: T) => FlowDirection | null) {
|
||||
const groups = new Map<string, OverlayRows<T>>()
|
||||
for (const row of rows) {
|
||||
if ((row.proto !== 47 && row.proto !== 50) || row.srcPort !== 0 || row.dstPort !== 0 ||
|
||||
row.packets <= 0 || isUnspecifiedIp(row.src) || isUnspecifiedIp(row.dst) || row.src === row.dst) continue
|
||||
const key = JSON.stringify([row.serverId, row.src, row.dst])
|
||||
const group = groups.get(key) ?? { gre: [], esp: [] }
|
||||
if (row.proto === 47) group.gre.push(row)
|
||||
else group.esp.push(row)
|
||||
groups.set(key, group)
|
||||
}
|
||||
|
||||
const copies = new Set<T>()
|
||||
const espDirections = new Map<T, FlowDirection>()
|
||||
const match = (gre: T[], esp: T[]) => {
|
||||
if (!gre.length || !esp.length) return
|
||||
const inner = account(gre), outer = account(esp)
|
||||
if (inner.packets !== outer.packets) return
|
||||
const overhead = (outer.bytes - inner.bytes) / inner.packets
|
||||
// Keep ambiguous records. An ESP envelope adds its headers, trailer and padding.
|
||||
if (overhead < 8 || overhead > 128) return
|
||||
const directions = new Set([...gre, ...esp].map(direction).filter(side => side !== null))
|
||||
if (directions.size > 1) return
|
||||
const [side] = directions
|
||||
if (side) for (const row of esp) espDirections.set(row, side)
|
||||
for (const row of gre) copies.add(row)
|
||||
}
|
||||
|
||||
for (const group of groups.values()) {
|
||||
if (!group.gre.length || !group.esp.length) continue
|
||||
const byMinute = new Map<string, OverlayRows<T>>()
|
||||
for (const plane of ["gre", "esp"] as const) for (const row of group[plane]) {
|
||||
const bucket = byMinute.get(row.bucketAt) ?? { gre: [], esp: [] }
|
||||
bucket[plane].push(row)
|
||||
byMinute.set(row.bucketAt, bucket)
|
||||
}
|
||||
const unmatched: OverlayRows<T> = { gre: [], esp: [] }
|
||||
for (const [, bucket] of [...byMinute].sort(([a], [b]) => a.localeCompare(b))) {
|
||||
match(bucket.gre, bucket.esp)
|
||||
if (!bucket.gre.length || !copies.has(bucket.gre[0])) {
|
||||
unmatched.gre.push(...bucket.gre)
|
||||
unmatched.esp.push(...bucket.esp)
|
||||
}
|
||||
}
|
||||
// The exporter can flush the two layers in different minute buckets.
|
||||
if (unmatched.gre.length && unmatched.esp.length &&
|
||||
Math.abs(Date.parse(unmatched.gre[0].bucketAt) - Date.parse(unmatched.esp[0].bucketAt)) <= 60_000 &&
|
||||
Math.abs(Date.parse(unmatched.gre.at(-1)!.bucketAt) - Date.parse(unmatched.esp.at(-1)!.bucketAt)) <= 60_000) {
|
||||
match(unmatched.gre, unmatched.esp)
|
||||
}
|
||||
}
|
||||
return { rows: rows.filter(row => !copies.has(row)), espDirections }
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
resetEngineForTests,
|
||||
} from "./traffic-flow-engine.js"
|
||||
import { factsSnapshotForTests } from "./traffic-flow-facts.js"
|
||||
import { classifyInternetBrand, mapInternetBrand, resolveInternetDest } from "./traffic-flow-dest.js"
|
||||
import { classifyInternetBrand, flowRateDirection, mapInternetBrand, resolveInternetDest } from "./traffic-flow-dest.js"
|
||||
import { disableCatalogFetchForTests, resetFlowCatalogForTests } from "./traffic-flow-classify.js"
|
||||
import {
|
||||
disableRipeEnqueueForTests,
|
||||
@@ -66,6 +66,11 @@ const topo: FlowTopology = {
|
||||
seedFlowTopologyForTests(topo)
|
||||
rememberServerIfaces(1, [{ name: "gre-client", ifindex: "2" }])
|
||||
|
||||
const publicFlow = { src: "95.167.1.10", dst: "8.8.8.8", inIface: "2", outIface: "ether1" }
|
||||
assert.equal(flowRateDirection(1, publicFlow, topo), "tx")
|
||||
assert.equal(flowRateDirection(1, { ...publicFlow, inIface: "ether1", outIface: "2" }, topo), "rx")
|
||||
assert.equal(flowRateDirection(1, publicFlow, null), null, "Public transit has no inferred download or upload")
|
||||
|
||||
ingestParsedFlowsForServerForTests(1, [
|
||||
{
|
||||
src: "95.167.1.10",
|
||||
|
||||
@@ -4,6 +4,9 @@ import { classifyFlowDst, type FlowClassification } from "./traffic-flow-classif
|
||||
import { resolveFlowIp } from "./traffic-flow-geoip.js"
|
||||
import { canonicalFactIface } from "./traffic-flow-ifindex.js"
|
||||
import {
|
||||
canonicalIp,
|
||||
isNonPublicIp,
|
||||
isUnspecifiedIp,
|
||||
resolveFlowEndpoints,
|
||||
type FlowEndpoints,
|
||||
type InternetDestCtx,
|
||||
@@ -15,6 +18,28 @@ import {
|
||||
type FlowTopology,
|
||||
} from "./traffic-flow-topology.js"
|
||||
|
||||
/** Aggregate DL/UL is relative to the exporter's WAN or its bound clients. */
|
||||
export function flowRateDirection(
|
||||
serverId: number,
|
||||
row: { src: string; dst: string; inIface: string; outIface: string },
|
||||
topo: FlowTopology | null,
|
||||
exporterHost?: string,
|
||||
): "rx" | "tx" | null {
|
||||
const ingress = canonicalFactIface(serverId, row.inIface)
|
||||
const egress = canonicalFactIface(serverId, row.outIface)
|
||||
const wan = topo?.wanIfaces.get(serverId)
|
||||
const clients = topo?.clientIfaces.get(serverId)
|
||||
const download = Boolean(wan?.has(ingress) || clients?.has(egress))
|
||||
const upload = Boolean(wan?.has(egress) || clients?.has(ingress))
|
||||
if (download !== upload) return download ? "rx" : "tx"
|
||||
if (isUnspecifiedIp(row.src) || isUnspecifiedIp(row.dst)) return null
|
||||
const host = canonicalIp(exporterHost)
|
||||
if (host && canonicalIp(row.src) === host && canonicalIp(row.dst) !== host) return "tx"
|
||||
if (host && canonicalIp(row.dst) === host && canonicalIp(row.src) !== host) return "rx"
|
||||
const srcLocal = isNonPublicIp(row.src), dstLocal = isNonPublicIp(row.dst)
|
||||
return srcLocal === dstLocal ? null : srcLocal ? "tx" : "rx"
|
||||
}
|
||||
|
||||
export interface InternetDestMeta {
|
||||
dest: string
|
||||
ripe: FlowIpMeta | null
|
||||
|
||||
@@ -12,6 +12,7 @@ import * as accumulator from "./traffic-flow-fact-accumulator.js"
|
||||
import * as serial from "./serial-operation.js"
|
||||
import * as parse from "./traffic-flow-parse.js"
|
||||
import * as ip from "./traffic-flow-ip.js"
|
||||
import * as ifindex from "./traffic-flow-ifindex.js"
|
||||
import { pruneRipeSqlite } from "./traffic-flow-ripe.js"
|
||||
import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js"
|
||||
|
||||
@@ -51,9 +52,9 @@ const dependencies:Record<string,unknown>={
|
||||
"./traffic-flow-ripe.js":{enqueueRipeMisses:()=>{throw Error("unexpected RIPE enqueue")},pruneRipeSqlite},
|
||||
"./traffic-flow-settings.js":{invalidateTrafficFlowSettingsCache:()=>{}},
|
||||
"./traffic-flow-ifaces.js":{maybeRefreshIfaces:async()=>{}},
|
||||
"./traffic-flow-ifindex.js":{canonicalFactIface:(_server:number,iface:string)=>iface},
|
||||
"./traffic-flow-ifindex.js":ifindex,
|
||||
"./traffic-flow-facts-filter.js":{shouldWriteFlowFact:()=>true},
|
||||
"./traffic-flow-dest.js":{resolveInternetDest:()=>({dest:"",ripe:null,classified:{category:"web",service:"https"},country:"XX",asn:0})},
|
||||
"./traffic-flow-dest.js":{flowRateDirection:()=>"tx",resolveInternetDest:()=>({dest:"",ripe:null,classified:{category:"web",service:"https"},country:"XX",asn:0})},
|
||||
"./traffic-flow-topology.js":{peekFlowTopology:()=>({}),peekServerCatalog:()=>({byId:new Map([[1,{type:"router"}]])}),getServerCatalog:()=>{throw Error("unexpected catalog SQL")},loadFlowTopology:()=>{throw Error("unexpected topology SQL")}},
|
||||
"./traffic-flow-facts.js":{
|
||||
bumpFlowFact:live.bump,hasPendingFlowFact:live.has,factsPendingSize:()=>live.size,
|
||||
|
||||
@@ -15,10 +15,10 @@ import { applicationName } from "./traffic-flow-apps.js"
|
||||
import { enqueueRipeMisses, pruneRipeSqlite } from "./traffic-flow-ripe.js"
|
||||
import { invalidateTrafficFlowSettingsCache } from "./traffic-flow-settings.js"
|
||||
import { maybeRefreshIfaces } from "./traffic-flow-ifaces.js"
|
||||
import { canonicalFactIface } from "./traffic-flow-ifindex.js"
|
||||
import { canonicalFactIface, resolveIfaceName } from "./traffic-flow-ifindex.js"
|
||||
import { shouldWriteFlowFact } from "./traffic-flow-facts-filter.js"
|
||||
import { canonicalIp } from "./traffic-flow-ip.js"
|
||||
import { resolveInternetDest } from "./traffic-flow-dest.js"
|
||||
import { flowRateDirection, resolveInternetDest } from "./traffic-flow-dest.js"
|
||||
import {
|
||||
getServerCatalog,
|
||||
loadFlowTopology,
|
||||
@@ -44,6 +44,9 @@ export const MAX_PENDING = 50_000
|
||||
export const DAILY_ASN_TOP = 500
|
||||
export const DAILY_RETENTION_DAYS = 396
|
||||
export const MINUTE_RETENTION_HOURS = 48
|
||||
export const OBSERVED_FLOW_IFACE_DIM = "observed-iface-v1"
|
||||
export const OBSERVED_FLOW_IFACE_COVERAGE_DIM = "observed-iface-coverage-v1"
|
||||
export const ADMITTED_FLOW_DAY_DIM = "admitted-v1"
|
||||
|
||||
let pendingCap = MAX_PENDING
|
||||
const ensuredParts = new Set<string>()
|
||||
@@ -315,12 +318,12 @@ function bumpTick(key: string, inBytes: number, outBytes: number): void {
|
||||
tickAccum.set(key, prev)
|
||||
}
|
||||
|
||||
function prepareFlowTicks(serverId:number,flow:ParsedFlow):Array<{key:string;inBytes:number;outBytes:number}> {
|
||||
function prepareFlowTicks(serverId:number,flow:ParsedFlow,direction:"rx"|"tx"|null):Array<{key:string;inBytes:number;outBytes:number}> {
|
||||
const plane=classifyFlowPlaneLite(flow)
|
||||
if(plane==="mgmt")return []
|
||||
return [{key:ringKey(serverId,plane==="overlay"?RING_OVERLAY:plane==="client_mesh"?RING_MESH:RING_PAYLOAD),inBytes:flow.bytes,outBytes:0},
|
||||
return [{key:ringKey(serverId,plane==="overlay"?RING_OVERLAY:plane==="client_mesh"?RING_MESH:RING_PAYLOAD),inBytes:direction==="rx"?flow.bytes:0,outBytes:direction==="tx"?flow.bytes:0},
|
||||
...(flow.inIface?[{key:ringKey(serverId,flow.inIface),inBytes:flow.bytes,outBytes:0}]:[]),
|
||||
...(flow.outIface&&flow.outIface!==flow.inIface?[{key:ringKey(serverId,flow.outIface),inBytes:0,outBytes:flow.bytes}]:[])]
|
||||
...(flow.outIface?[{key:ringKey(serverId,flow.outIface),inBytes:0,outBytes:flow.bytes}]:[])]
|
||||
}
|
||||
|
||||
function emptyRing(): { inBps: number[]; outBps: number[] } {
|
||||
@@ -426,21 +429,36 @@ export function queueParsedFlows(serverId: number, flows: ParsedFlowInput[]): vo
|
||||
const topo = peekFlowTopology(), catalog = peekServerCatalog()
|
||||
if (!topo) refreshColdTopology()
|
||||
if (!catalog) refreshColdCatalog()
|
||||
const serverType = catalog?.byId.get(serverId)?.type
|
||||
const server = catalog?.byId.get(serverId)
|
||||
const serverType = server?.type
|
||||
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})
|
||||
const classified=destMeta.classified,country=destMeta.country
|
||||
const seenIfaces = new Map<string, string>()
|
||||
let interfaceUncertain = false
|
||||
for (const rawIface of [flow.inIface, flow.outIface]) {
|
||||
const {name, index} = resolveIfaceName(serverId, rawIface)
|
||||
if (name === "—") continue
|
||||
if (index && name === `#${index}`) interfaceUncertain = true
|
||||
if (index || !seenIfaces.has(name)) seenIfaces.set(name, index ? String(Number(index)) : name)
|
||||
}
|
||||
// A cold index can be an alias of another endpoint. Keep that minute out of complete interface totals.
|
||||
const observedDimensions = interfaceUncertain ? [] : [
|
||||
{dim:OBSERVED_FLOW_IFACE_COVERAGE_DIM,key:"all"},
|
||||
...[...seenIfaces.values()].map(key => ({dim:OBSERVED_FLOW_IFACE_DIM,key})),
|
||||
]
|
||||
const dims=[{dim:"proto",key:protoName(flow.proto)},{dim:"app",key:applicationName(flow.proto,flow.dstPort,flow.srcPort)},
|
||||
{dim:"iface",key:flow.inIface||"__unknown__"},{dim:"category",key:classified.category},{dim:"service",key:classified.service},
|
||||
...(country?[{dim:"country",key:country}]:[]),{dim:"asn",key:destMeta.asn?String(destMeta.asn):"unknown"}].map(row=>({...row,cacheKey:dimKey(serverId,bucketAt,row.dim,row.key)}))
|
||||
...observedDimensions,
|
||||
{dim:"country",key:country||"unknown"},{dim:"asn",key:destMeta.asn?String(destMeta.asn):"unknown"}].map(row=>({...row,cacheKey:dimKey(serverId,bucketAt,row.dim,row.key)}))
|
||||
const fact:FactRow|null=shouldWriteFlowFact({serverId,serverType,inIface:flow.inIface,outIface:flow.outIface,proto:flow.proto,
|
||||
srcPort:flow.srcPort,dstPort:flow.dstPort,src:flow.src,dst:flow.dst,topo,dest:destMeta.dest,
|
||||
natSrc:flow.natSrc,natDst:flow.natDst,natSrcPort:flow.natSrcPort,natDstPort:flow.natDstPort})?
|
||||
{serverId,bucketAt:hourAt,iface:canonicalFactIface(serverId,flow.inIface),country:country||"XX",service:classified.service,
|
||||
asn:destMeta.asn,bytes:flow.bytes,packets:flow.packets}:null
|
||||
const ticks=prepareFlowTicks(serverId,flow),rollupCacheKey=rollupKey(serverId,bucketAt)
|
||||
const ticks=prepareFlowTicks(serverId,flow,flowRateDirection(serverId,flow,topo,server?.host)),rollupCacheKey=rollupKey(serverId,bucketAt)
|
||||
if(!admitAggregateRecord(rollupCacheKey,flow,dims,fact,ticks,observedAt))continue
|
||||
for(const tick of ticks)bumpTick(tick.key,tick.inBytes,tick.outBytes)
|
||||
bumpRollup(serverId,bucketAt,flow,flow.bytes,flow.packets,rollupCacheKey)
|
||||
@@ -557,7 +575,7 @@ function pruneRecent(sinceMs = Date.now() - LIVE_WINDOW_MS): void {
|
||||
}
|
||||
}
|
||||
|
||||
export interface PendingFlowReadOptions { serverId?:number; sinceIso?:string; untilIso?:string }
|
||||
export interface PendingFlowReadOptions { serverId?:number; serverIds?:readonly number[]; sinceIso?:string; untilIso?:string }
|
||||
export class FlowPendingReadUnconfirmed extends Error {
|
||||
readonly code = "FLOW_PENDING_UNCONFIRMED"
|
||||
constructor(readonly batchState:"running"|"unknown") { super(`NetFlow pending read unavailable: ${batchState} write batch`) }
|
||||
@@ -566,8 +584,10 @@ function pendingReadMatches(options:PendingFlowReadOptions):(row:Pick<PendingFlo
|
||||
const since=options.sinceIso===undefined?-Infinity:typeof options.sinceIso==="string"?Date.parse(options.sinceIso):NaN
|
||||
const until=options.untilIso===undefined?Infinity:typeof options.untilIso==="string"?Date.parse(options.untilIso):NaN
|
||||
if(options.serverId!==undefined&&(!Number.isSafeInteger(options.serverId)||options.serverId<=0)||Number.isNaN(since)||Number.isNaN(until)||since>until)throw Error("Invalid pending flow read scope")
|
||||
const serverIds = options.serverIds && new Set(options.serverIds)
|
||||
return row=>{
|
||||
if(options.serverId!==undefined&&row.serverId!==options.serverId)return false
|
||||
if(serverIds&&!serverIds.has(row.serverId))return false
|
||||
const time=Date.parse(row.bucketAt)
|
||||
if(!Number.isFinite(time))throw Error("Invalid pending flow timestamp")
|
||||
return time>=since&&time<=until
|
||||
@@ -748,6 +768,15 @@ async function upsertMinuteAndDaily(rollup:Map<string,MinuteRollup>,dims:Map<str
|
||||
uniqueDst: acc.dsts.size,
|
||||
conversations: acc.conversations,
|
||||
})
|
||||
const day = dayKey(bucketAt ?? "")
|
||||
await ensureParentPartition("flow_daily_dims", day)
|
||||
await dbQuery(`
|
||||
INSERT INTO flow_daily_dims (server_id, day, dim, key, bytes, packets)
|
||||
VALUES (?, ?, ?, 'all', ?, ?)
|
||||
ON CONFLICT(server_id, day, dim, key) DO UPDATE SET
|
||||
bytes = flow_daily_dims.bytes + excluded.bytes,
|
||||
packets = flow_daily_dims.packets + excluded.packets
|
||||
`, [Number(serverIdRaw), day, ADMITTED_FLOW_DAY_DIM, acc.bytes, acc.packets])
|
||||
}
|
||||
for (const [k, acc] of dims) {
|
||||
const [serverIdRaw, bucketAt, dim, key] = k.split("\0")
|
||||
|
||||
@@ -12,19 +12,35 @@ function source(names:string[]):string {
|
||||
}
|
||||
function fixture() {
|
||||
const original=Error("original ring validation"),ackError=Error("original transport failure"),events:string[]=[]
|
||||
let badRing=false,badAck=false,quality:unknown=null
|
||||
let badRing=false,badAck=false,quality:unknown=null,now=0
|
||||
const run=new Function("deps",`
|
||||
const exports={},flowIngestQualitySchema=deps.schema,HEARTBEAT_UNAVAILABLE="unavailable";
|
||||
let lastHeartbeat=null,heartbeatFailure=null,state={bound:false,address:null},restartAttempts=4,worker={};
|
||||
const FLOW_HEARTBEAT_MAX_AGE_MS=15000,performance={now:deps.now};
|
||||
let lastHeartbeat=null,lastHeartbeatAt=-Infinity,heartbeatFailure=null,state={bound:false,address:null},restartAttempts=4,worker={};
|
||||
const applyRingSnapshot=deps.consume,markFlowRingSnapshotUnavailable=deps.invalidate,postToWorker=deps.send;
|
||||
const getEngineStats=()=>({pendingSize:0,dropped:0,packetsReceived:0});
|
||||
const getTrafficFlowSettingsRow=async()=>({packetsReceived:77,lastExporterIp:null,lastError:null,lastDatagramAt:null});
|
||||
${source(["applyWorkerHeartbeat","rejectWorkerHeartbeat","handleWorkerMessage","getFlowWorkerHealth","getFlowRuntimeCounters","applyHeartbeatForTests"])}
|
||||
${source(["applyWorkerHeartbeat","rejectWorkerHeartbeat","handleWorkerMessage","getFlowListenerState","hasCurrentFlowHeartbeat","getFlowWorkerHealth","getFlowRuntimeCounters","applyHeartbeatForTests"])}
|
||||
return {receive:handleWorkerMessage,local:applyHeartbeatForTests,inspect:()=>({lastHeartbeat,heartbeatFailure,state}),health:getFlowWorkerHealth,counters:getFlowRuntimeCounters};
|
||||
`)({schema:flowIngestQualitySchema,consume:(_rows:unknown,data:unknown)=>{events.push("consume");if(badRing)throw original;quality=data},invalidate:()=>{events.push("invalidate");quality=null},send:()=>{events.push("ack");if(badAck)throw ackError}})
|
||||
`)({now:()=>now,schema:flowIngestQualitySchema,consume:(_rows:unknown,data:unknown)=>{events.push("consume");if(badRing)throw original;quality=data},invalidate:()=>{events.push("invalidate");quality=null},send:()=>{events.push("ack");if(badAck)throw ackError}})
|
||||
const payload=(packetsReceived=11)=>({bound:true,address:"127.0.0.1:2055",packetsReceived,lastExporterIp:"127.0.0.1",lastError:"",lastDatagramAt:"2026-10-29T10:00:00.000Z",pendingSize:1,dropped:0,rowsStored:1,workerAlive:true,rings:[],dataQuality:createFlowIngestQuality().snapshot()})
|
||||
return {run,payload,events,original,ackError,ring:(value:boolean)=>{badRing=value},ack:(value:boolean)=>{badAck=value},quality:()=>quality}
|
||||
return {run,payload,events,original,ackError,ring:(value:boolean)=>{badRing=value},ack:(value:boolean)=>{badAck=value},quality:()=>quality,advance:(ms:number)=>{now+=ms}}
|
||||
}
|
||||
test("a stale heartbeat keeps the last observed counters but cannot report a live listener", async () => {
|
||||
const f = fixture()
|
||||
f.run.receive({type:"heartbeat",heartbeatId:"first",payload:f.payload()})
|
||||
assert.equal((await f.run.counters()).collectorCountersSource, "live")
|
||||
f.advance(15_001)
|
||||
assert.equal(f.run.health().bound, false)
|
||||
assert.equal(f.run.health().alive, false)
|
||||
const counters = await f.run.counters()
|
||||
assert.equal(counters.packetsReceived, 11)
|
||||
assert.equal(counters.collectorCountersSource, "last-known")
|
||||
assert.equal(counters.dataQuality, null)
|
||||
f.run.receive({type:"heartbeat",heartbeatId:"next",payload:f.payload(12)})
|
||||
assert.equal(f.run.health().bound, true)
|
||||
assert.equal((await f.run.counters()).collectorCountersSource, "live")
|
||||
})
|
||||
test("ring refusal is caught, marks observation unavailable, preserves original error/counters and ACKs consumption",async()=>{
|
||||
const f=fixture();f.run.receive({type:"heartbeat",heartbeatId:"first",payload:f.payload()});f.events.length=0;f.ring(true)
|
||||
assert.doesNotThrow(()=>f.run.receive({type:"heartbeat",heartbeatId:"bad",payload:f.payload(999)}))
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
getFlowListenerState,
|
||||
getFlowRuntimeCounters,
|
||||
getFlowWorkerHealth,
|
||||
readTrafficFlowSettingsStatus,
|
||||
ingestParsedFlowsForServerForTests,
|
||||
lastFlushUsedTransactionForTests,
|
||||
maybeRefreshIfaces,
|
||||
@@ -148,10 +149,16 @@ applyHeartbeatForTests({
|
||||
})
|
||||
assert.equal(getFlowListenerState().bound, true)
|
||||
assert.equal((await getFlowRuntimeCounters()).packetsReceived, 42)
|
||||
assert.equal((await getFlowRuntimeCounters()).collectorCountersSource, "live")
|
||||
const settingsStatus = await readTrafficFlowSettingsStatus()
|
||||
assert.equal(settingsStatus.collectorCountersSource, "live")
|
||||
assert.equal(settingsStatus.packetsReceived, 42)
|
||||
assert.equal(settingsStatus.listenerBound, true)
|
||||
assert.equal(getFlowWorkerHealth().alive, false)
|
||||
setWantListenForTests(true)
|
||||
assert.equal(simulateWorkerExitForTests(), 1)
|
||||
assert.equal(getFlowListenerState().bound, false)
|
||||
assert.equal((await getFlowRuntimeCounters()).collectorCountersSource, "last-known")
|
||||
setWantListenForTests(false)
|
||||
|
||||
resetFlowRingsForTests()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { flowReadBucketSnapshot } from "./traffic-flow-read-policy.js"
|
||||
import { FlowSourceWindowTooLarge, FlowSqlSourceTooLarge } from "./traffic-flow-read-window.js"
|
||||
import {flowIngestQualitySchema} from "@mmapp/contracts/traffic-flow"
|
||||
import { flowLifecycleBoundary, type FlowLifecycleAuthorization } from "./traffic-flow-lifecycle-fence.js"
|
||||
import { flowCounter } from "./traffic-flow-observed-values.js"
|
||||
@@ -39,6 +40,7 @@ import {
|
||||
getTrafficFlowSettingsRow,
|
||||
listHostPeers,
|
||||
invalidateTrafficFlowSettingsCache,
|
||||
toTrafficFlowSettingsDto,
|
||||
} from "./traffic-flow-settings.js"
|
||||
import { projectFlowTalkers } from "./traffic-flow-talkers-model.js"
|
||||
import { resolveIfaceName } from "./traffic-flow-ifaces.js"
|
||||
@@ -66,7 +68,9 @@ let listenerGeneration=0
|
||||
let restartTimer: ReturnType<typeof setTimeout> | null = null
|
||||
let restartAttempts = 0
|
||||
let lastHeartbeat: CollectorHeartbeat | null = null
|
||||
let lastHeartbeatAt = -Infinity
|
||||
let heartbeatFailure:Error|null=null
|
||||
const FLOW_HEARTBEAT_MAX_AGE_MS = 15_000
|
||||
const HEARTBEAT_UNAVAILABLE="NetFlow: недостоверный снимок коллектора; наблюдения недоступны"
|
||||
let state: FlowListenerState = { bound: false, address: null }
|
||||
let wantListen = false
|
||||
@@ -126,6 +130,7 @@ function applyWorkerHeartbeat(payload: CollectorHeartbeat): void {
|
||||
const dataQuality=payload.dataQuality==null?null:flowIngestQualitySchema.parse(payload.dataQuality)
|
||||
applyRingSnapshot(payload.rings,dataQuality)
|
||||
lastHeartbeat = {...payload,dataQuality,rings:[]}
|
||||
lastHeartbeatAt = performance.now()
|
||||
heartbeatFailure=null
|
||||
state = { bound: payload.bound, address: payload.address }
|
||||
restartAttempts = 0
|
||||
@@ -150,7 +155,7 @@ function handleWorkerMessage(msg: WorkerToMain): void {
|
||||
}
|
||||
if (msg.type === "error") {
|
||||
lastHeartbeat = lastHeartbeat
|
||||
? { ...lastHeartbeat, lastError: msg.payload.message, workerAlive: true }
|
||||
? { ...lastHeartbeat, lastError: msg.payload.message }
|
||||
: null
|
||||
}
|
||||
}
|
||||
@@ -214,6 +219,7 @@ async function spawnWorker(authorize?: FlowLifecycleAuthorization): Promise<void
|
||||
if (worker !== w) return
|
||||
worker = null
|
||||
state = { bound: false, address: null }
|
||||
if (lastHeartbeat) lastHeartbeat = { ...lastHeartbeat, workerAlive: false, bound: false }
|
||||
if (!wantListen || startupPending) return
|
||||
const delay = Math.min(30_000, 1000 * 2 ** restartAttempts)
|
||||
restartAttempts += 1
|
||||
@@ -249,6 +255,7 @@ function stopWorkerProcess(authorize?: FlowLifecycleAuthorization): void {
|
||||
// The sync public stop returns immediately; starts and DB operations drain it.
|
||||
void workerLifecycle.retire(current,authorize)
|
||||
worker = null
|
||||
if (lastHeartbeat) lastHeartbeat = { ...lastHeartbeat, workerAlive: false, bound: false }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,15 +281,19 @@ export function setWantListenForTests(value: boolean): void {
|
||||
}
|
||||
|
||||
export function getFlowListenerState(): FlowListenerState {
|
||||
return state
|
||||
return state.bound && !hasCurrentFlowHeartbeat() ? { bound: false, address: null } : state
|
||||
}
|
||||
|
||||
function hasCurrentFlowHeartbeat(): boolean {
|
||||
return Boolean(lastHeartbeat?.workerAlive) && heartbeatFailure === null && performance.now() - lastHeartbeatAt <= FLOW_HEARTBEAT_MAX_AGE_MS
|
||||
}
|
||||
|
||||
export function getFlowWorkerHealth(): FlowWorkerHealth {
|
||||
const hb = lastHeartbeat
|
||||
const mem = getEngineStats()
|
||||
return {
|
||||
alive: heartbeatFailure===null&&Boolean(worker) && (hb?.workerAlive ?? false),
|
||||
bound: state.bound,
|
||||
alive: Boolean(worker) && hasCurrentFlowHeartbeat(),
|
||||
bound: getFlowListenerState().bound,
|
||||
pendingSize: hb?.pendingSize ?? mem.pendingSize,
|
||||
dropped: hb?.dropped ?? mem.dropped,
|
||||
packetsReceived: hb?.packetsReceived ?? mem.packetsReceived,
|
||||
@@ -292,17 +303,20 @@ export function getFlowWorkerHealth(): FlowWorkerHealth {
|
||||
export async function getFlowRuntimeCounters() {
|
||||
const hb = lastHeartbeat
|
||||
if (hb) {
|
||||
const current = hasCurrentFlowHeartbeat()
|
||||
return {
|
||||
collectorCountersSource: current ? "live" as const : "last-known" as const,
|
||||
packetsReceived: hb.packetsReceived,
|
||||
lastExporterIp: hb.lastExporterIp ?? null,
|
||||
lastError: heartbeatFailure?HEARTBEAT_UNAVAILABLE:hb.lastError || null,
|
||||
lastError: heartbeatFailure || hb.workerAlive && !current ? HEARTBEAT_UNAVAILABLE : hb.lastError || null,
|
||||
lastDatagramAt: hb.lastDatagramAt ?? null,
|
||||
dropped: hb.dropped ?? 0,
|
||||
dataQuality:hb.dataQuality??null,
|
||||
dataQuality:current ? hb.dataQuality ?? null : null,
|
||||
}
|
||||
}
|
||||
const settings = await getTrafficFlowSettingsRow()
|
||||
return {
|
||||
collectorCountersSource: "last-known" as const,
|
||||
packetsReceived: settings.packetsReceived,
|
||||
lastExporterIp: settings.lastExporterIp ?? null,
|
||||
lastError: heartbeatFailure?HEARTBEAT_UNAVAILABLE:settings.lastError || null,
|
||||
@@ -312,6 +326,13 @@ export async function getFlowRuntimeCounters() {
|
||||
}
|
||||
}
|
||||
|
||||
export async function readTrafficFlowSettingsStatus() {
|
||||
const settings = await toTrafficFlowSettingsDto(getFlowListenerState())
|
||||
const { packetsReceived, lastExporterIp, lastError, lastDatagramAt, dataQuality, collectorCountersSource } = await getFlowRuntimeCounters()
|
||||
const { bound, address } = getFlowListenerState()
|
||||
return { ...settings, packetsReceived, lastExporterIp, lastError, lastDatagramAt, dataQuality, collectorCountersSource, listenerBound: bound, listenerAddress: address }
|
||||
}
|
||||
|
||||
export async function startTrafficFlowListener(authorize?: FlowLifecycleAuthorization) {
|
||||
if (databaseStop) throw new Error("NetFlow listener is paused for a database operation")
|
||||
// Fence the internal stop and flush separately. The ordinary synchronous stop
|
||||
@@ -475,12 +496,17 @@ interface StoredBucketRow {
|
||||
nat_dst_port: number | null
|
||||
}
|
||||
|
||||
export async function listStoredFlowRows(sinceIso: string, serverId?: number, options?: { maxRows?: number; untilIso?: string }): Promise<PendingFlowRow[]> {
|
||||
export async function listStoredFlowRows(sinceIso: string, serverId?: number, options?: { maxRows?: number; untilIso?: string; serverIds?: readonly number[] }): Promise<PendingFlowRow[]> {
|
||||
if (options?.maxRows !== undefined && (!Number.isSafeInteger(options.maxRows) || options.maxRows < 1 || options.maxRows > 100000)) throw new Error("Некорректный предел flow rows")
|
||||
if (options?.untilIso && !Number.isFinite(Date.parse(options.untilIso))) throw new Error("Некорректная граница flow window")
|
||||
const settings = options?.maxRows === undefined ? await getTrafficFlowSettingsRow() : await readFlowSourceLimits()
|
||||
const keep = Math.max(20, settings.topN)
|
||||
const parameters: Array<string | number> = serverId === undefined ? [sinceIso, keep] : [sinceIso, keep, serverId]
|
||||
const serverIds = options?.serverIds
|
||||
if (serverIds !== undefined && (serverId !== undefined || serverIds.some(id => !Number.isSafeInteger(id) || id < 1))) throw new Error("Некорректная область flow rows")
|
||||
if (serverIds?.length === 0) return []
|
||||
const parameters: Array<string | number | readonly number[]> = [sinceIso, keep]
|
||||
if (serverId !== undefined || serverIds !== undefined) parameters.push(serverId ?? serverIds!)
|
||||
const serverFilter = serverId !== undefined ? "AND server_id=$3" : serverIds ? "AND server_id=ANY($3::bigint[])" : ""
|
||||
const untilParameter = options?.untilIso ? parameters.push(options.untilIso) : undefined
|
||||
if (options?.maxRows !== undefined) parameters.push(options.maxRows + 1)
|
||||
const stored = await flowReadBucketSnapshot<StoredBucketRow>(`
|
||||
@@ -495,12 +521,15 @@ export async function listStoredFlowRows(sinceIso: string, serverId?: number, op
|
||||
PARTITION BY server_id, bucket_at ORDER BY bytes DESC
|
||||
) AS rn
|
||||
FROM flow_buckets fb
|
||||
WHERE bucket_at >= $1 ${serverId === undefined ? "" : "AND server_id=$3"} ${untilParameter ? `AND bucket_at <= $${untilParameter}` : ""}
|
||||
WHERE bucket_at >= $1 ${serverFilter} ${untilParameter ? `AND bucket_at <= $${untilParameter}` : ""}
|
||||
) ranked
|
||||
WHERE rn <= $2
|
||||
${options?.maxRows !== undefined ? `LIMIT $${parameters.length}` : ""}
|
||||
`, parameters)
|
||||
if (options?.maxRows !== undefined && stored.length > options.maxRows) throw new Error("Превышен предел flow rows; сократите окно или число серверов")
|
||||
`, parameters).catch(error => {
|
||||
if (error instanceof FlowSqlSourceTooLarge) throw new FlowSourceWindowTooLarge()
|
||||
throw error
|
||||
})
|
||||
if (options?.maxRows !== undefined && stored.length > options.maxRows) throw new FlowSourceWindowTooLarge()
|
||||
const merged = new Map<string, PendingFlowRow>()
|
||||
for (const r of stored) {
|
||||
mergeInto(merged, {
|
||||
@@ -525,30 +554,33 @@ export async function listStoredFlowRows(sinceIso: string, serverId?: number, op
|
||||
})
|
||||
}
|
||||
if (!worker) {
|
||||
for (const p of peekPendingFlows({serverId,sinceIso,untilIso:options?.untilIso})) {
|
||||
for (const p of peekPendingFlows({serverId,serverIds,sinceIso,untilIso:options?.untilIso})) {
|
||||
if (p.bucketAt < sinceIso || options?.untilIso && p.bucketAt > options.untilIso || serverId !== undefined && p.serverId !== serverId) continue
|
||||
flowCounter(p.bytes); flowCounter(p.packets)
|
||||
mergeInto(merged, p)
|
||||
}
|
||||
}
|
||||
const result = capFlowRowsPerServerBucket([...merged.values()], keep)
|
||||
if (options?.maxRows !== undefined && result.length > options.maxRows) throw new Error("Превышен предел flow rows с pending; сократите окно или число серверов")
|
||||
if (options?.maxRows !== undefined && result.length > options.maxRows) throw new FlowSourceWindowTooLarge()
|
||||
return result
|
||||
}
|
||||
|
||||
export async function listFlowRowsForWindow(minutes: number, serverId?: number, options?: { maxRows?: number; untilIso?: string }): Promise<PendingFlowRow[]> {
|
||||
const sinceIso = new Date(Date.now() - minutes * 60_000).toISOString()
|
||||
const end = options?.untilIso === undefined ? Date.now() : Date.parse(options.untilIso)
|
||||
if (!Number.isFinite(end)) throw new Error("Некорректная граница flow window")
|
||||
const sinceIso = new Date(end - minutes * 60_000).toISOString()
|
||||
// History remains available while the collector starts or is stopped; pending rows are merged below.
|
||||
return await listStoredFlowRows(sinceIso, serverId, options)
|
||||
}
|
||||
|
||||
export async function listFlowTalkers(minutes = 5): Promise<FlowStatsDto> {
|
||||
const settings = await getTrafficFlowSettingsRow()
|
||||
const runtime = await getFlowRuntimeCounters()
|
||||
const rows = await listFlowRowsForWindow(minutes, undefined, { untilIso: new Date().toISOString() })
|
||||
const catalog = await getServerCatalog()
|
||||
const nameById = new Map([...catalog.byId].map(([id, server]) => [id, server.name]))
|
||||
const projection = projectFlowTalkers(rows, minutes, settings.topN, nameById, resolveIfaceName)
|
||||
const runtime = await getFlowRuntimeCounters()
|
||||
const listener = getFlowListenerState()
|
||||
return {
|
||||
exportersOnline: projection.observedExporters,
|
||||
bytesPerMin: projection.bytesPerMin,
|
||||
@@ -557,12 +589,13 @@ export async function listFlowTalkers(minutes = 5): Promise<FlowStatsDto> {
|
||||
topProto: projection.topProto,
|
||||
talkers: projection.talkers,
|
||||
dataQuality:runtime.dataQuality,
|
||||
collectorCountersSource: runtime.collectorCountersSource,
|
||||
lastExporterIp: runtime.lastExporterIp,
|
||||
lastError: runtime.lastError,
|
||||
packetsReceived: runtime.packetsReceived,
|
||||
lastDatagramAt: runtime.lastDatagramAt,
|
||||
listenerBound: state.bound,
|
||||
listenerAddress: state.address,
|
||||
listenerBound: listener.bound,
|
||||
listenerAddress: listener.address,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import { applySqlMigrations } from '../db/migrate.js'
|
||||
import { ensurePartitionFor } from '../db/partitions.js'
|
||||
import { createMcpConnection,updateMcpSettings,setConnectionStatus } from '../modules/mcp/store.js'
|
||||
import { mcpConnectionCreateSchema } from '@mmapp/contracts/mcp'
|
||||
import type { FlowAnalyticsDto } from '@mmapp/contracts/traffic-flow'
|
||||
import mcpTransport from '../routes/mcp-transport.js'
|
||||
import trafficFlowRoutes from '../routes/traffic-flow.js'
|
||||
import logsRoutes from '../routes/logs.js'
|
||||
@@ -16,8 +17,11 @@ import trafficRoutes from '../routes/traffic.js'
|
||||
import { flowReadBudget } from './traffic-flow-read-policy.js'
|
||||
import { resetFlowRingsForTests,applyHeartbeatForTests } from './traffic-flow-ingest.js'
|
||||
import { createFlowIngestQuality } from './traffic-flow-ingest-quality.js'
|
||||
import { queueParsedFlows, strictDrainEngine } from './traffic-flow-engine.js'
|
||||
import { rememberServerIfaces } from './traffic-flow-ifindex.js'
|
||||
import { disableRipeEnqueueForTests } from './traffic-flow-ripe.js'
|
||||
import { readFlowCatalogWindow } from './traffic-flow-catalog-read.js'
|
||||
import { invalidateFlowAnalyticsCache } from './traffic-flow-analytics.js'
|
||||
import { sharedDatabaseGate } from './shared-database-gate.js'
|
||||
import { getTrafficFlowSettingsRow,invalidateTrafficFlowSettingsCache } from './traffic-flow-settings.js'
|
||||
import { createFlowReadBudget,createFlowReadResponseOwner } from './traffic-flow-read-budget.js'
|
||||
@@ -35,12 +39,13 @@ try{
|
||||
await pool.query("INSERT INTO servers(id,name,host) VALUES(1,'visible','192.0.2.1'),(2,'FORBIDDEN_HIDDEN_SERVER','192.0.2.2')")
|
||||
const bucket=new Date(Date.now()-60000);await ensurePartitionFor(pool,'flow_buckets','day',bucket)
|
||||
await pool.query("INSERT INTO flow_buckets(server_id,bucket_at,src,dst,proto,src_port,dst_port,bytes,packets,in_iface) VALUES(1,$1,'198.18.1.1','8.8.8.8',6,1,443,100,1,'ether1'),(2,$1,'198.18.2.1','8.8.8.8',6,1,443,999,1,'FORBIDDEN_HIDDEN_IFACE')",[bucket])
|
||||
const access=await createMcpConnection(mcpConnectionCreateSchema.parse({name:'owned HTTP',mode:'observer',permissions:['mm:traffic:read'],expiresAt:new Date(Date.now()+600000).toISOString(),scope:{serverIds:[1],operationIds:['traffic.flow.talkers.read','traffic.flow.monthly.read','traffic.flow.live.read'],allowGlobal:false},limits:{callsPerMinute:240}}),actor)
|
||||
const access=await createMcpConnection(mcpConnectionCreateSchema.parse({name:'owned HTTP',mode:'observer',permissions:['mm:traffic:read'],expiresAt:new Date(Date.now()+600000).toISOString(),scope:{serverIds:[1],operationIds:['traffic.flow.talkers.read','traffic.flow.monthly.read','traffic.flow.live.read','traffic.flow.analytics.read'],allowGlobal:false},limits:{callsPerMinute:240}}),actor)
|
||||
resetFlowRingsForTests();disableRipeEnqueueForTests()
|
||||
const collectorQuality=createFlowIngestQuality();collectorQuality.reject(987654,987,'aggregate-capacity')
|
||||
// Synthetic quality goes through the actual validated heartbeat receiver;
|
||||
// this test starts no collector Worker or remote exporter.
|
||||
applyHeartbeatForTests({bound:false,address:null,packetsReceived:0,lastExporterIp:'',lastError:'',lastDatagramAt:null,pendingSize:0,dropped:0,rowsStored:0,workerAlive:true,rings:[],dataQuality:collectorQuality.snapshot()})
|
||||
const heartbeat={bound:false,address:null,packetsReceived:0,lastExporterIp:'',lastError:'',lastDatagramAt:null,pendingSize:0,dropped:0,rowsStored:0,workerAlive:true,rings:[],dataQuality:collectorQuality.snapshot()}
|
||||
applyHeartbeatForTests(heartbeat)
|
||||
await readFlowCatalogWindow(5,[1])
|
||||
{
|
||||
const budget=createFlowReadBudget({maxOwners:2,maxReservedBytes:200}),response=new EventEmitter() as ServerResponse,owner=createFlowReadResponseOwner(response,budget,{maxOwners:1,maxReservedBytes:100})
|
||||
@@ -119,6 +124,139 @@ try{
|
||||
const scopedQuality=(result.structuredContent as {result:Record<string,unknown>}).result
|
||||
assert.equal(scopedQuality.dataQuality,null);assert.equal(scopedQuality.collectorQualityUnavailableReason,'global-telemetry-not-granted')
|
||||
assert.ok(!JSON.stringify(result).includes('987654'),'Scoped read must not disclose collector-wide loss amounts')
|
||||
const oldFlowStart = new Date(Date.now() - 7 * 3600000)
|
||||
await ensurePartitionFor(pool, 'flow_buckets', 'day', oldFlowStart)
|
||||
await ensurePartitionFor(pool, 'flow_buckets', 'day', new Date(oldFlowStart.getTime() + 251 * 60000))
|
||||
await pool.query(`INSERT INTO flow_buckets(server_id,bucket_at,src,dst,proto,src_port,dst_port,bytes,packets,in_iface)
|
||||
SELECT 1,$1::timestamptz + minutes * interval '1 minute','198.18.3.0'::inet + host, '8.8.4.4',6,1234,443,50,1,'ether1'
|
||||
FROM generate_series(0,251) minutes CROSS JOIN generate_series(1,200) host`, [oldFlowStart])
|
||||
const denseRest = await fetch(endpoint+'/api/traffic/flow?range=24h')
|
||||
assert.equal(denseRest.status,200)
|
||||
const restWindow = (await denseRest.json() as {sourceWindow:{requestedMinutes:number;effectiveMinutes:number}}).sourceWindow
|
||||
assert.equal(restWindow.requestedMinutes,1440)
|
||||
assert(restWindow.effectiveMinutes<1440)
|
||||
const denseMcp = await client.callTool({name:'mm_netflow_talkers_get',arguments:{serverId:1,minutes:1440}})
|
||||
assert.ok(!denseMcp.isError,JSON.stringify(denseMcp))
|
||||
const mcpDay = (denseMcp.structuredContent as {result:{sourceWindow:{requestedMinutes:number;effectiveMinutes:number}; retainedRowsBytes:number; from:string;to:string}}).result
|
||||
assert.equal(mcpDay.sourceWindow.requestedMinutes,1440)
|
||||
assert(mcpDay.sourceWindow.effectiveMinutes<1440)
|
||||
assert.equal(Date.parse(mcpDay.to)-Date.parse(mcpDay.from),mcpDay.sourceWindow.effectiveMinutes*60000)
|
||||
assert(!JSON.stringify(denseMcp).includes('FORBIDDEN_'))
|
||||
console.log('PASS dense REST and MCP NetFlow windows report the actual safely admitted interval')
|
||||
const counterStart = new Date(Date.now()-10*60000)
|
||||
await ensurePartitionFor(pool,'traffic_samples','week',counterStart)
|
||||
await ensurePartitionFor(pool,'traffic_samples','week',new Date(counterStart.getTime()+50010))
|
||||
await pool.query(`INSERT INTO traffic_samples(server_id,interface_name,sampled_at,rx_bps,tx_bps,rx_bytes,tx_bytes)
|
||||
SELECT 1,'gre-test',$1::timestamptz + n * interval '1 millisecond',n,2*n,3*n,4*n FROM generate_series(1,50010) n`,[counterStart])
|
||||
await pool.query(`INSERT INTO flow_buckets(server_id,bucket_at,src,dst,proto,src_port,dst_port,bytes,packets,in_iface)
|
||||
VALUES(1,$1,'8.8.8.8','198.18.1.1',6,443,1,50,1,'gre-test')`,[bucket])
|
||||
applyHeartbeatForTests({...heartbeat,dataQuality:createFlowIngestQuality().snapshot()})
|
||||
try {
|
||||
const analytics = await fetch(endpoint+'/api/traffic/flow/analytics?range=5m&serverId=1')
|
||||
assert.equal(analytics.status,200,await analytics.clone().text())
|
||||
const shortReport = await analytics.json() as {bpsWire:number;bytesWire:number;sourceWindow?:unknown;rxSeries:number[];txSeries:number[]}
|
||||
assert.equal(shortReport.bpsWire,150030)
|
||||
assert.equal(shortReport.bytesWire,350070)
|
||||
assert.equal(shortReport.sourceWindow,undefined)
|
||||
const report = await client.callTool({name:'mm_netflow_analytics_report_get',arguments:{serverId:1,minutes:5}})
|
||||
assert.ok(!report.isError,JSON.stringify(report))
|
||||
const mcp = (report.structuredContent as {result:{analytics:{rxSeries:number[];txSeries:number[]}}}).result.analytics
|
||||
for (const series of [shortReport,mcp]) {
|
||||
assert.equal(series.rxSeries.reduce((sum,value)=>sum+value,0),50*8/60/1e6)
|
||||
assert.equal(series.txSeries.reduce((sum,value)=>sum+value,0),100*8/60/1e6)
|
||||
}
|
||||
} finally {
|
||||
applyHeartbeatForTests(heartbeat)
|
||||
invalidateFlowAnalyticsCache()
|
||||
}
|
||||
console.log('PASS five-minute REST analysis uses one latest counter per interface despite 50,010 historical samples')
|
||||
const overlayBucket = new Date(Math.floor(Date.now()/60000)*60000-120000)
|
||||
await ensurePartitionFor(pool,'flow_buckets','day',overlayBucket)
|
||||
await pool.query(`INSERT INTO flow_buckets(server_id,bucket_at,src,dst,proto,src_port,dst_port,bytes,packets,in_iface,out_iface) VALUES
|
||||
(1,$1,'192.0.2.1','203.0.113.9',47,0,0,12000,10,'gre-test','ether1'),
|
||||
(1,$1,'192.0.2.1','203.0.113.9',50,0,0,12400,10,'ether1','ether1'),
|
||||
(1,$1,'203.0.113.9','192.0.2.1',47,0,0,24000,20,'ether1','gre-test'),
|
||||
(1,$1,'203.0.113.9','192.0.2.1',50,0,0,24800,20,'ether1','ether1'),
|
||||
(1,$1,'203.0.113.10','192.0.2.1',47,0,0,1000,1,'ether1','gre-other'),
|
||||
(1,$1,'203.0.113.11','203.0.113.12',50,0,0,2000,2,'ether1','ether1')`,[overlayBucket])
|
||||
try {
|
||||
const args={serverId:1,interface:'ether1',dedup:false,excludeOverlay:false}
|
||||
const rest=await fetch(endpoint+'/api/traffic/flow/analytics?range=5m&serverId=1&iface=ether1&dedup=false&excludeOverlay=false')
|
||||
assert.equal(rest.status,200,await rest.clone().text())
|
||||
const filtered=await client.callTool({name:'mm_netflow_analytics_report_get',arguments:{...args,minutes:5}})
|
||||
const live=await client.callTool({name:'mm_netflow_live_observe',arguments:args})
|
||||
assert.ok(!filtered.isError,JSON.stringify(filtered))
|
||||
assert.ok(!live.isError,JSON.stringify(live))
|
||||
const mcp=(filtered.structuredContent as {result:{analytics:FlowAnalyticsDto}}).result.analytics
|
||||
const sample=(live.structuredContent as {result:{events:{data:FlowAnalyticsDto}[]}}).result.events[0].data
|
||||
const fromSeries=(series:number[])=>Math.round(series.reduce((sum,value)=>sum+value,0)*60*1e6/8)
|
||||
for(const analytics of [await rest.json() as FlowAnalyticsDto,mcp,sample]){
|
||||
assert.equal(analytics.bytes,40300,'Each overlay packet sequence contributes only the ESP bytes')
|
||||
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.equal(sample.bpsOverlay,40200*8/300)
|
||||
}finally{
|
||||
await pool.query("DELETE FROM flow_buckets WHERE server_id=1 AND bucket_at=$1 AND proto IN (47,50)",[overlayBucket])
|
||||
invalidateFlowAnalyticsCache()
|
||||
}
|
||||
console.log('PASS REST and MCP history and live analytics collapse GRE/ESP and place each confirmed ESP direction once')
|
||||
const completedMinute=Math.floor(Date.now()/60000)*60000-180000
|
||||
rememberServerIfaces(1,[{name:'ether-audit',ifindex:'77'}])
|
||||
const wallClock=Date.now
|
||||
try {
|
||||
Date.now=()=>completedMinute+1000
|
||||
queueParsedFlows(1,Array.from({length:211},(_,i)=>({
|
||||
src:'192.0.2.1',dst:'198.18.1.1',proto:6,srcPort:1000+i,dstPort:443,
|
||||
bytes:i<200?100:10,packets:1,inIface:i===210?'ether-audit':'ether-heavy',outIface:i<200?'vxlan':'77',
|
||||
})))
|
||||
} finally {Date.now=wallClock}
|
||||
await strictDrainEngine(async()=>{},[1])
|
||||
const analyticsUrl=endpoint+'/api/traffic/flow/analytics?range=5m&serverId=1&excludeOverlay=false'
|
||||
const ifaceUrl=analyticsUrl+'&iface=ether-audit'
|
||||
const readAnalytics=async(url:string)=>{
|
||||
const response=await fetch(url)
|
||||
assert.equal(response.status,200,await response.clone().text())
|
||||
return await response.json() as FlowAnalyticsDto
|
||||
}
|
||||
const admitted=await readAnalytics(analyticsUrl)
|
||||
assert.equal(admitted.timeBasis,'collector-receipt')
|
||||
assert.equal(admitted.admittedTotals?.bytes,20110,'the minute aggregate includes every admitted record beyond top-N')
|
||||
assert.equal(admitted.admittedTotals?.packets,211)
|
||||
const smallInterface=await readAnalytics(ifaceUrl)
|
||||
assert.equal(smallInterface.bytes,0,'all detail rows for the small interface are below top-N')
|
||||
assert.equal(smallInterface.admittedTotals?.bytes,110,'egress is included and the same ingress/egress interface is counted once')
|
||||
assert.equal(smallInterface.admittedTotals?.packets,11)
|
||||
const aggregateReport=await client.callTool({name:'mm_netflow_analytics_report_get',arguments:{serverId:1,minutes:5,interface:'ether-audit'}})
|
||||
assert.ok(!aggregateReport.isError,JSON.stringify(aggregateReport))
|
||||
assert.equal((aggregateReport.structuredContent as {result:{analytics:FlowAnalyticsDto}}).result.analytics.admittedTotals?.bytes,110)
|
||||
await pool.query("UPDATE flow_minute_stats SET bytes=bytes+1 WHERE server_id=1 AND bucket_at=$1",[new Date(completedMinute)])
|
||||
invalidateFlowAnalyticsCache()
|
||||
assert.equal((await readAnalytics(ifaceUrl)).admittedTotals,undefined,'mixed old/new writes in one minute must not look complete')
|
||||
assert.equal((await readAnalytics(analyticsUrl)).admittedTotals?.bytes,20111)
|
||||
|
||||
const writerMonth=new Date(completedMinute).toISOString().slice(0,7)
|
||||
const authoritativeMonthly=await fetch(endpoint+'/api/traffic/flow/monthly?month='+writerMonth+'&serverId=1')
|
||||
assert.equal(authoritativeMonthly.status,200,await authoritativeMonthly.clone().text())
|
||||
const auditedMonth=await authoritativeMonthly.json() as {bytes:number;totalsSource:string}
|
||||
assert.equal(auditedMonth.bytes,20110)
|
||||
assert.equal(auditedMonth.totalsSource,'admitted-daily')
|
||||
|
||||
const monthBucket=new Date('2026-09-01T00:00:00Z')
|
||||
await ensurePartitionFor(pool,'flow_daily_dims','month',monthBucket)
|
||||
await pool.query(`INSERT INTO flow_daily_dims(server_id,day,dim,key,bytes,packets) VALUES
|
||||
(1,$1,'country','US',80,8),(1,$1,'country','unknown',20,2),
|
||||
(1,$1,'service','web',120,12),(1,$1,'asn','unknown',200,20),(2,$1,'asn','unknown',900,90),
|
||||
(1,$1::date+1,'service','web',40,4)`,[monthBucket])
|
||||
const monthly=await fetch(endpoint+'/api/traffic/flow/monthly?month=2026-09&serverId=1')
|
||||
assert.equal(monthly.status,200,await monthly.clone().text())
|
||||
const monthlyDto=await monthly.json() as {bytes:number;totalsSource:string;countries:{id:string;bytes:number;packets:number}[]}
|
||||
assert.equal(monthlyDto.bytes,240)
|
||||
assert.equal(monthlyDto.totalsSource,'legacy-dimensions')
|
||||
assert.equal(monthlyDto.countries.find(row=>row.id==='unknown')?.bytes,160)
|
||||
assert.equal(monthlyDto.countries.find(row=>row.id==='unknown')?.packets,16)
|
||||
console.log('PASS complete archived and per-interface totals survive top-N, mixed legacy minutes stay partial, and monthly unknown geography is counted')
|
||||
const globalAccess=await createMcpConnection(mcpConnectionCreateSchema.parse({name:'owned global telemetry',mode:'observer',permissions:['mm:traffic:read'],expiresAt:new Date(Date.now()+600000).toISOString(),scope:{serverIds:[1],operationIds:['traffic.flow.talkers.read','netflow.settings.read'],allowGlobal:true},limits:{callsPerMinute:240}}),actor)
|
||||
const globalClient=new Client({name:'owned-global-quality',version:'1'})
|
||||
try{
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
export class FlowSqlSourceTooLarge extends Error {
|
||||
readonly statusCode = 409
|
||||
readonly code = "STATE_UNAVAILABLE"
|
||||
|
||||
constructor() {
|
||||
super("Источник NetFlow превышает допустимый объём; сократите область или окно")
|
||||
}
|
||||
}
|
||||
export class FlowSourceWindowTooLarge extends FlowSqlSourceTooLarge {}
|
||||
|
||||
type FlowReadWindow = { requestedMinutes: number; effectiveMinutes: number }
|
||||
|
||||
/** Retrying a smaller historical window keeps the same response reservation.
|
||||
* Only an explicit source-size refusal allows fallback; other errors propagate. */
|
||||
export async function readFlowWindow<T extends object>(requestedMinutes: number, read: (minutes: number) => Promise<T>, signal?: AbortSignal): Promise<T & { sourceWindow?: FlowReadWindow }> {
|
||||
let effectiveMinutes = requestedMinutes
|
||||
for (;;) {
|
||||
signal?.throwIfAborted()
|
||||
try {
|
||||
const result = await read(effectiveMinutes)
|
||||
return effectiveMinutes === requestedMinutes ? result : { ...result, sourceWindow: { requestedMinutes, effectiveMinutes } }
|
||||
} catch (error) {
|
||||
if (!(error instanceof FlowSourceWindowTooLarge) || effectiveMinutes <= 1) throw error
|
||||
effectiveMinutes = Math.max(1, Math.floor(effectiveMinutes / (effectiveMinutes <= 15 ? 2 : 4)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -247,7 +247,6 @@ export function enGreIfaceNames(topo: FlowTopology, serverId: number, ifaceNames
|
||||
export async function latestWireBps(serverId: number, ifaceNames: string[], bounded = false): Promise<{ bps: number; bytes: number }> {
|
||||
if (!ifaceNames.length) return { bps: 0, bytes: 0 }
|
||||
if (bounded && ifaceNames.length > 1000) throw new Error("Превышен предел физических интерфейсов NetFlow")
|
||||
const placeholders = ifaceNames.map(() => "?").join(",")
|
||||
const rows = await flowReadDbAll<{
|
||||
name: string
|
||||
rxBps: number
|
||||
@@ -255,18 +254,18 @@ export async function latestWireBps(serverId: number, ifaceNames: string[], boun
|
||||
rxBytes: number
|
||||
txBytes: number
|
||||
}>(`
|
||||
SELECT ${bounded ? "DISTINCT ON (interface_name)" : ""} interface_name AS name, rx_bps AS "rxBps", tx_bps AS "txBps", rx_bytes AS "rxBytes", tx_bytes AS "txBytes"
|
||||
FROM traffic_samples
|
||||
WHERE server_id = ? AND interface_name IN (${placeholders})
|
||||
ORDER BY ${bounded ? "interface_name," : ""} sampled_at DESC
|
||||
`, [serverId, ...ifaceNames])
|
||||
const seen = new Set<string>()
|
||||
SELECT iface.name, sample.rx_bps AS "rxBps", sample.tx_bps AS "txBps", sample.rx_bytes AS "rxBytes", sample.tx_bytes AS "txBytes"
|
||||
FROM (SELECT DISTINCT unnest(?::text[]) AS name) iface
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT rx_bps, tx_bps, rx_bytes, tx_bytes FROM traffic_samples
|
||||
WHERE server_id = ? AND interface_name = iface.name
|
||||
ORDER BY sampled_at DESC LIMIT 1
|
||||
) sample
|
||||
`, [ifaceNames, serverId])
|
||||
let bps = 0
|
||||
let bytes = 0
|
||||
for (const r of rows) {
|
||||
if (bounded && ([r.rxBps,r.txBps].some(value => value === null || !Number.isFinite(Number(value)) || Number(value) < 0 || Number(value) > Number.MAX_SAFE_INTEGER) || [r.rxBytes,r.txBytes].some(value => value === null || !Number.isSafeInteger(Number(value)) || Number(value) < 0))) throw new Error("Не подтверждены физические счётчики NetFlow")
|
||||
if (seen.has(r.name)) continue
|
||||
seen.add(r.name)
|
||||
bps += (Number(r.rxBps) || 0) + (Number(r.txBps) || 0)
|
||||
bytes += (Number(r.rxBytes) || 0) + (Number(r.txBytes) || 0)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { serverSnapshots, type Server } from "../db/schema.js"
|
||||
import { readServerSamplesInRange, type TrafficHistoryReadOptions } from "./traffic-collector.js"
|
||||
import { readTrafficCollectionContext } from "./traffic-collection-state.js"
|
||||
import { applyTrafficFreshness } from "./traffic-freshness.js"
|
||||
import { buildTrafficFromSamples, isLoopbackName } from "./traffic-rate.js"
|
||||
import { buildTrafficFromSamples, isLoopbackName, trafficHistoryBoundaryLimitMs } from "./traffic-rate.js"
|
||||
|
||||
export type TrafficHistoryServer = Pick<Server, "id" | "name" | "host" | "site" | "country" | "enabled" | "updatedAt">
|
||||
type CollectionContext = Awaited<ReturnType<typeof readTrafficCollectionContext>>
|
||||
@@ -12,25 +12,39 @@ type CollectionContext = Awaited<ReturnType<typeof readTrafficCollectionContext>
|
||||
/** Shared application interpretation of stored counter deltas; no device polling. */
|
||||
export async function readServerTrafficHistory(server: TrafficHistoryServer, rangeStartMs: number, rangeEndMs: number, onlyInterface?: string,
|
||||
options: TrafficHistoryReadOptions = {}, context?: CollectionContext) {
|
||||
const [rows, snapshots, collection] = await Promise.all([
|
||||
readServerSamplesInRange(server.id, new Date(rangeStartMs).toISOString(), { ...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString() }),
|
||||
return (await readServerTrafficHistoryWithSeries(server, rangeStartMs, rangeEndMs, onlyInterface, options, context)).history
|
||||
}
|
||||
|
||||
export async function readServerTrafficHistoryWithSeries(server: TrafficHistoryServer, rangeStartMs: number, rangeEndMs: number, onlyInterface?: string,
|
||||
options: TrafficHistoryReadOptions = {}, context?: CollectionContext) {
|
||||
const collection = context ?? await readTrafficCollectionContext([server.id])
|
||||
const boundaryLimitMs = trafficHistoryBoundaryLimitMs(collection.settings?.intervalSec)
|
||||
const [rows, snapshots] = await Promise.all([
|
||||
readServerSamplesInRange(server.id, new Date(rangeStartMs).toISOString(), {
|
||||
...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString(),
|
||||
interfaceNames: onlyInterface === undefined ? undefined : [onlyInterface], interfaceCountersOnly: true,
|
||||
precedingMaxIntervalMs: boundaryLimitMs,
|
||||
}),
|
||||
db.select({ status: serverSnapshots.status, polledAt: serverSnapshots.polledAt }).from(serverSnapshots).where(eq(serverSnapshots.serverId, server.id)).orderBy(desc(serverSnapshots.polledAt)).limit(1),
|
||||
context ?? readTrafficCollectionContext([server.id]),
|
||||
])
|
||||
const built = buildTrafficFromSamples(rows, rangeStartMs, rangeEndMs, onlyInterface, undefined, true)
|
||||
const series = buildTrafficFromSamples(rows, rangeStartMs, rangeEndMs, onlyInterface, undefined, true, boundaryLimitMs)
|
||||
if (series.observations) series.observations = applyTrafficFreshness(series.observations, server, collection.settings, collection.states.get(server.id), rangeEndMs)
|
||||
const status = snapshots[0]?.status === "offline" ? "offline" as const : snapshots[0]?.status === "online" ? "online" as const : "degraded" as const
|
||||
return {
|
||||
const history = {
|
||||
volumeUnit: "GiB" as const, id: String(server.id), name: server.name || server.host, site: server.site || "—", country: server.country || "UN", status,
|
||||
observations: built.observations && applyTrafficFreshness(built.observations, server, collection.settings, collection.states.get(server.id), rangeEndMs),
|
||||
rxNow: built.rxNow, txNow: built.txNow, rxPeak: built.rxPeak, txPeak: built.txPeak,
|
||||
rxTotal: built.rxTotalGiB, txTotal: built.txTotalGiB, sessions: built.sessions, rxSeries: built.rxSeries, txSeries: built.txSeries,
|
||||
observations: series.observations,
|
||||
rxNow: series.rxNow, txNow: series.txNow, rxPeak: series.rxPeak, txPeak: series.txPeak,
|
||||
rxTotal: series.rxTotalGiB, txTotal: series.txTotalGiB, sessions: series.sessions, rxSeries: series.rxSeries, txSeries: series.txSeries,
|
||||
}
|
||||
return { history, series }
|
||||
}
|
||||
|
||||
/** Legacy interface DTO plus independent evidence for nullable MCP state. */
|
||||
export async function readServerTrafficInterfaces(server: TrafficHistoryServer, rangeEndMs = Date.now(), options: TrafficHistoryReadOptions = {}) {
|
||||
const rangeStartMs = rangeEndMs - 86400000
|
||||
const [rows, context] = await Promise.all([readServerSamplesInRange(server.id, new Date(rangeStartMs).toISOString(), { ...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString() }), readTrafficCollectionContext([server.id])])
|
||||
const [rows, context] = await Promise.all([readServerSamplesInRange(server.id, new Date(rangeStartMs).toISOString(), {
|
||||
...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString(), interfaceCountersOnly: true,
|
||||
}), readTrafficCollectionContext([server.id])])
|
||||
const byIface = new Map<string, typeof rows>()
|
||||
for (const row of rows) {
|
||||
if (isLoopbackName(row.interfaceName) || row.peerPublicKey) continue
|
||||
|
||||
@@ -34,6 +34,8 @@ else {
|
||||
await pool.query("DELETE FROM traffic_samples WHERE server_id=$1 AND (flags & 4)=0",[serverId])
|
||||
await pool.query("UPDATE traffic_samples SET sampled_at=$1 WHERE server_id=$2",[new Date(Date.now()-30_000).toISOString(),serverId])
|
||||
counter="3751000";const success=await collectTrafficOnce();dto=await detail();assert.equal(dto.observations.currentState,"current");assert.equal(dto.observations.rxNow,true);assert(dto.rxNow>0.9);cases++
|
||||
const overview=await get("/traffic/servers?range=1h")
|
||||
assert.deepEqual(overview.peaks,{rx:dto.rxPeak,tx:dto.txPeak});cases++
|
||||
await pool.query("INSERT INTO scheduler_runs(id,job_key,started_at,finished_at,status,result_json) VALUES($1,'traffic',$2,$3,'ok',$4::jsonb)",[prefix+"run",success.sampledAt,new Date().toISOString(),JSON.stringify(success)])
|
||||
assert.equal((await buildSignalSnapshotFromCollectors()).trafficByServer.length,1);cases++
|
||||
const succeeded=dto.observations.lastSucceededAt
|
||||
|
||||
@@ -172,3 +172,16 @@ for (const minutes of [5, 15]) {
|
||||
const weighted = observed([observationSample(0,0,0),observationSample(500,62_500,62_500),observationSample(1500,312_500,312_500)])
|
||||
assert.equal(weighted.rxSeries[0], 1.5)
|
||||
assert.equal(weighted.rxSeries[1], 2)
|
||||
|
||||
const boundaryRows = [observationSample(-15_000, 0, 0), observationSample(15_000, 3_750_000, 1_875_000),
|
||||
observationSample(45_000, 7_500_000, 3_750_000), observationSample(75_000, 11_250_000, 5_625_000)]
|
||||
const boundary = observed(boundaryRows)
|
||||
assert.equal(boundary.rxTotalGiB, 5_625_000 / 1024 ** 3)
|
||||
assert.equal(boundary.txTotalGiB, 2_812_500 / 1024 ** 3)
|
||||
assert(boundary.observations?.rxSeries.slice(0, 45).every(Boolean))
|
||||
assert(boundary.observations?.rxSeries.slice(45).every(value => !value), "History does not extend past the last poll inside the range")
|
||||
const lostBoundary = observed([boundaryRows[0], { ...boundaryRows[1], continuityKnown: false }, ...boundaryRows.slice(2)])
|
||||
assert.equal(lostBoundary.rxTotalGiB, 3_750_000 / 1024 ** 3)
|
||||
assert(lostBoundary.observations?.rxSeries.slice(0, 15).every(value => !value))
|
||||
const staleBoundary = observed([observationSample(-100_000, 0, 0), ...boundaryRows.slice(1)])
|
||||
assert.equal(staleBoundary.rxTotalGiB, 3_750_000 / 1024 ** 3)
|
||||
@@ -146,6 +146,10 @@ export function sampleSeriesKey(interfaceName: string, peerPublicKey = ""): stri
|
||||
return `${interfaceName}\0${peerPublicKey}`
|
||||
}
|
||||
|
||||
export function trafficHistoryBoundaryLimitMs(intervalSec = 30): number {
|
||||
return Math.max(5, intervalSec) * 3000
|
||||
}
|
||||
|
||||
export function buildTrafficFromSamples(
|
||||
rows: TrafficSampleLike[],
|
||||
rangeStartMs: number,
|
||||
@@ -153,6 +157,7 @@ export function buildTrafficFromSamples(
|
||||
onlyInterface?: string | readonly string[],
|
||||
peerPublicKey?: string,
|
||||
includeObservations=false,
|
||||
boundaryLimitMs = trafficHistoryBoundaryLimitMs(),
|
||||
): BuiltTrafficSeries {
|
||||
const empty: BuiltTrafficSeries = {
|
||||
rxNow: 0,
|
||||
@@ -206,9 +211,10 @@ export function buildTrafficFromSamples(
|
||||
continue
|
||||
}
|
||||
|
||||
const sorted = [...arr].sort((a, b) => Date.parse(a.sampledAt) - Date.parse(b.sampledAt))
|
||||
const sorted = arr.filter(row => Date.parse(row.sampledAt) <= rangeEndMs)
|
||||
.sort((a, b) => Date.parse(a.sampledAt) - Date.parse(b.sampledAt))
|
||||
const last = sorted[sorted.length - 1]
|
||||
if (!last) continue
|
||||
if (!last || Date.parse(last.sampledAt) < rangeStartMs) continue
|
||||
for (const row of sorted) {
|
||||
const t = Date.parse(row.sampledAt)
|
||||
if (!Number.isFinite(t) || t < rangeStartMs || t > rangeEndMs) continue
|
||||
@@ -230,15 +236,16 @@ export function buildTrafficFromSamples(
|
||||
const t0 = parseIsoMs(prev.sampledAt)
|
||||
const t1 = parseIsoMs(cur.sampledAt)
|
||||
if(Number.isFinite(Date.parse(cur.sampledAt))&&t1>=rangeStartMs&&t1<=rangeEndMs)lastAttemptAt=Math.max(lastAttemptAt??t1,t1)
|
||||
if (t1 <= rangeStartMs || (t0 < rangeStartMs && t1 - t0 > boundaryLimitMs)) continue
|
||||
const sameIdentity = (prev.nativeCounterIdentity ?? null) === (cur.nativeCounterIdentity ?? null)
|
||||
const identityPresent = cur.continuityKnown === undefined || typeof cur.nativeCounterIdentity === "string" && cur.nativeCounterIdentity.length > 0
|
||||
const countersKnown = prev.countersKnown !== false && cur.countersKnown !== false && cur.continuityKnown !== false && identityPresent && sameIdentity
|
||||
const rxBps = countersKnown ? rateBpsFromDelta(prev.rxBytes, cur.rxBytes, t0, t1) : null
|
||||
const txBps = countersKnown ? rateBpsFromDelta(prev.txBytes, cur.txBytes, t0, t1) : null
|
||||
if (t1 >= rangeStartMs && t1 <= rangeEndMs) {
|
||||
if (rxBps !== null) rxBytesDelta += cur.rxBytes - prev.rxBytes
|
||||
if (txBps !== null) txBytesDelta += cur.txBytes - prev.txBytes
|
||||
}
|
||||
// Attribute only the in-window fraction of a measured interval and never extend beyond its next poll.
|
||||
const overlap = (t1 - Math.max(t0, rangeStartMs)) / (t1 - t0)
|
||||
if (rxBps !== null) rxBytesDelta += (cur.rxBytes - prev.rxBytes) * overlap
|
||||
if (txBps !== null) txBytesDelta += (cur.txBytes - prev.txBytes) * overlap
|
||||
if (rxBps == null && txBps == null) continue
|
||||
const rxMbps = bpsToMbps(rxBps ?? 0)
|
||||
const txMbps = bpsToMbps(txBps ?? 0)
|
||||
|
||||
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict'
|
||||
import fs from 'node:fs'
|
||||
import vm from 'node:vm'
|
||||
import ts from 'typescript'
|
||||
import {buildTrafficFromSamples,mergeBuiltTraffic} from './traffic-rate.js'
|
||||
import {buildTrafficFromSamples,mergeBuiltTraffic,trafficHistoryBoundaryLimitMs} from './traffic-rate.js'
|
||||
import {applyTrafficFreshness,trafficBindingStatus} from './traffic-freshness.js'
|
||||
|
||||
const source=fs.readFileSync(new URL('./traffic-users.ts',import.meta.url),'utf8')
|
||||
@@ -14,7 +14,7 @@ let calls=0,fail=false
|
||||
const at=Date.parse('2026-10-13T00:00:00Z')
|
||||
const users=Array.from({length:100},(_,i)=>({id:String(i),login:'user'+i,name:'User '+i,role:'user',active:true,
|
||||
bindings:[{serverId:1,interfaceName:'ether1',peerPublicKey:'',userId:String(i),id:'binding'+i}]}))
|
||||
const context=vm.createContext({Date,Map,Promise,buildTrafficFromSamples,mergeBuiltTraffic,applyTrafficFreshness,trafficBindingStatus,
|
||||
const context=vm.createContext({Date,Map,Promise,buildTrafficFromSamples,mergeBuiltTraffic,trafficHistoryBoundaryLimitMs,applyTrafficFreshness,trafficBindingStatus,
|
||||
listUsers:async()=>users,servers:{},db:{select:()=>({from:async()=>[{id:1,enabled:true,updatedAt:new Date(at).toISOString()}]})},
|
||||
readTrafficCollectionContext:async()=>({settings:{enabled:true,intervalSec:30},states:new Map()}),
|
||||
readServerSamplesInRange:async()=>{calls++;await new Promise(resolve=>setTimeout(resolve,5));if(fail)throw Error('fixture read failed');
|
||||
|
||||
@@ -6,6 +6,7 @@ import { readServerSamplesInRange, type TrafficHistoryReadOptions } from "./traf
|
||||
import {
|
||||
buildTrafficFromSamples,
|
||||
mergeBuiltTraffic,
|
||||
trafficHistoryBoundaryLimitMs,
|
||||
type BuiltTrafficSeries,
|
||||
} from "./traffic-rate.js"
|
||||
import { readTrafficCollectionContext } from "./traffic-collection-state.js"
|
||||
@@ -82,6 +83,13 @@ export async function buildUserTrafficList(rangeStartMs: number, rangeEndMs: num
|
||||
const sinceIso = new Date(rangeStartMs).toISOString()
|
||||
const users = options.users ?? await listUsers()
|
||||
const [context, allServers] = await Promise.all([readTrafficCollectionContext(options.serverIds), options.serverIds === undefined ? db.select().from(servers) : db.select().from(servers).where(inArray(servers.id, [...options.serverIds]))])
|
||||
const boundaryLimitMs = trafficHistoryBoundaryLimitMs(context.settings?.intervalSec)
|
||||
const interfacesByServer = new Map<number, Set<string>>()
|
||||
for (const user of users) for (const binding of user.bindings) {
|
||||
const names = interfacesByServer.get(binding.serverId) ?? new Set<string>()
|
||||
names.add(binding.interfaceName)
|
||||
interfacesByServer.set(binding.serverId, names)
|
||||
}
|
||||
const catalog = new Map(allServers.map(s => [s.id, s]))
|
||||
const sampleCache = new Map<number, ReturnType<typeof readServerSamplesInRange>>()
|
||||
|
||||
@@ -93,18 +101,21 @@ export async function buildUserTrafficList(rangeStartMs: number, rangeEndMs: num
|
||||
let pending = sampleCache.get(b.serverId)
|
||||
if (!pending) {
|
||||
if (options.serverIds !== undefined && !options.serverIds.includes(b.serverId)) throw new Error("Привязка вне выбранной области истории")
|
||||
pending = readServerSamplesInRange(b.serverId, sinceIso, { ...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString() })
|
||||
pending = readServerSamplesInRange(b.serverId, sinceIso, {
|
||||
...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString(),
|
||||
interfaceNames: [...interfacesByServer.get(b.serverId)!], precedingMaxIntervalMs: boundaryLimitMs,
|
||||
})
|
||||
sampleCache.set(b.serverId, pending)
|
||||
}
|
||||
const rows = await pending
|
||||
const peerKey = b.peerPublicKey ?? ""
|
||||
const built = buildTrafficFromSamples(rows, rangeStartMs, rangeEndMs, b.interfaceName, peerKey, true)
|
||||
const built = buildTrafficFromSamples(rows, rangeStartMs, rangeEndMs, b.interfaceName, peerKey, true, boundaryLimitMs)
|
||||
const server = catalog.get(b.serverId)
|
||||
if (built.observations) built.observations = applyTrafficFreshness(built.observations,
|
||||
server ?? { enabled: false, updatedAt: "" }, context.settings, context.states.get(b.serverId), rangeEndMs)
|
||||
parts.push(built)
|
||||
const last = [...rows.filter((r) =>
|
||||
r.interfaceName === b.interfaceName && (r.peerPublicKey ?? "") === peerKey,
|
||||
r.interfaceName === b.interfaceName && (r.peerPublicKey ?? "") === peerKey && Date.parse(r.sampledAt) >= rangeStartMs,
|
||||
)]
|
||||
.sort((a, c) => Date.parse(a.sampledAt) - Date.parse(c.sampledAt))
|
||||
.at(-1)
|
||||
|
||||
@@ -163,7 +163,7 @@ for(const mode of ['runtime-policy-entry','policy-authorize','executor-entry','e
|
||||
const error=Object.assign(Error('PRIVATE_RUNTIME_POLICY_FAILURE'),{code:'ECONNRESET'}),events=[]
|
||||
const env={failure:error,failAfterWrapper:mode==='after-wrapper',productionVrf:mode!=='non-production',
|
||||
operationId:id,connection:{owner_id:'owner',id:'connection'},serverIds:[1],evidence:{objects:[],snapshots:[{resource:'vrf',serverId:1,sourceProof:'a'.repeat(64)}]},serviceExecution:undefined,reservedStep:{},
|
||||
plan:{id:'plan',operation_id:'ros.vrf.create',operation_version:1,approved_by:'owner'},step:{kind:'ros',step:{serverId:1,resource:'vrf',action:'create',fields:{name:'customers',interfaces:'none'}}},
|
||||
plan:{id:'plan',operation_id:'ros.vrf.create',operation_version:1,approved_by:'owner',evidence:{sealed:'fixture-ciphertext'}},step:{kind:'ros',step:{serverId:1,resource:'vrf',action:'create',fields:{name:'customers',interfaces:'none'}}},
|
||||
prefix:{async check(){}},async authorize(){},async authorizeCatalog(){if(mode==='policy-authorize')throw error},McpError,createVrfRuntimeDiagnosticBoundary:diagnostic.createVrfRuntimeDiagnosticBoundary,
|
||||
console:{error(raw){events.push(JSON.parse(raw));if(loggerThrows)throw Error('PRIVATE_LOGGER_FAILURE')}},
|
||||
async executeRosStep(){throw error},
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -8,6 +8,7 @@ import { ensurePartitionFor } from "../db/partitions.js"
|
||||
import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js"
|
||||
import { invokeMcpTool } from "../modules/mcp/runtime.js"
|
||||
import { buildFlowAnalytics, invalidateFlowAnalyticsCache } from "../services/traffic-flow-analytics.js"
|
||||
import { FlowSourceWindowTooLarge } from "../services/traffic-flow-read-window.js"
|
||||
import { loadFlowTopology, invalidateFlowCatalogCache } from "../services/traffic-flow-topology.js"
|
||||
import { resetFlowRingsForTests as clearPendingFlows } from "../services/traffic-flow-ingest.js"
|
||||
import { disableRipeEnqueueForTests, resetRipeCacheForTests } from "../services/traffic-flow-ripe.js"
|
||||
@@ -53,7 +54,7 @@ export async function verifyMcpFlowAnalytics(actor: AuthUser) {
|
||||
await pool.query("DELETE FROM flow_buckets WHERE server_id=110 AND in_iface='analytics-duplicate-in'")
|
||||
await assert.rejects(read({userId:users[1]})); await assert.rejects(read({userId:users[2]}))
|
||||
await assert.rejects(read({serverId:111})); await assert.rejects(read({minutes:1441})); await assert.rejects(read({interface:"__all__"})); await assert.rejects(read({skipHeavy:true}))
|
||||
await assert.rejects(buildFlowAnalytics({serverId:110,minutes:5},{serverIds:[110],maxRows:1,authorize:async()=>{}}),/предел/)
|
||||
await assert.rejects(buildFlowAnalytics({serverId:110,minutes:5},{serverIds:[110],maxRows:1,authorize:async()=>{}}),FlowSourceWindowTooLarge)
|
||||
const cancelled = new AbortController(); cancelled.abort(); await assert.rejects(invokeMcpTool(principal,"mm_netflow_analytics_report_get",{serverId:110},cancelled.signal))
|
||||
const duringQuery = (match: string, action: () => Promise<unknown>) =>
|
||||
duringFlowSourceQuery(pool, match, action, () => assert.rejects(read()))
|
||||
|
||||
@@ -10,6 +10,7 @@ import { ensurePartitionFor } from "../db/partitions.js"
|
||||
import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js"
|
||||
import { invokeMcpTool } from "../modules/mcp/runtime.js"
|
||||
import { buildFlowMapHops, invalidateFlowMapHopsCache } from "../services/traffic-flow-map-hops.js"
|
||||
import { FlowSourceWindowTooLarge } from "../services/traffic-flow-read-window.js"
|
||||
import { invalidateFlowCatalogCache } from "../services/traffic-flow-topology.js"
|
||||
import { resetFlowRingsForTests, ingestParsedFlowsForServerForTests } from "../services/traffic-flow-ingest.js"
|
||||
import { disableRipeEnqueueForTests, resetRipeCacheForTests } from "../services/traffic-flow-ripe.js"
|
||||
@@ -57,7 +58,7 @@ export async function verifyMcpFlowMap(actor: AuthUser) {
|
||||
ingestParsedFlowsForServerForTests(113,[{src:"198.18.240.6",dst:"8.8.4.4",proto:6,srcPort:1,dstPort:443,bytes:999,packets:1,inIface:"NEVER_MAP_PENDING",outIface:""}])
|
||||
assert.equal((await read()).map.totalBytes,600)
|
||||
for (const input of [{userId:users[2]},{userId:users[3]},{serverId:113},{minutes:1441},{interface:"__all__"},{minSharePct:0},{nestedLimit:51}]) await assert.rejects(read(input))
|
||||
await assert.rejects(buildFlowMapHops({serverId:112,minutes:5},{serverIds:[112],maxRows:1,authorize:async()=>{}}),/предел/)
|
||||
await assert.rejects(buildFlowMapHops({serverId:112,minutes:5},{serverIds:[112],maxRows:1,authorize:async()=>{}}),FlowSourceWindowTooLarge)
|
||||
const cancelled=new AbortController();cancelled.abort();await assert.rejects(invokeMcpTool(principal,"mm_netflow_map_hops_get",{serverId:112},cancelled.signal))
|
||||
const duringQuery = (match: string, action: () => Promise<unknown>) =>
|
||||
duringFlowSourceQuery(pool, match, action, () => assert.rejects(read()))
|
||||
|
||||
@@ -38,6 +38,10 @@ export async function verifyMcpTrafficHistory(actor: AuthUser) {
|
||||
assert.equal((await readTrafficCollectionContext([])).states.size, 0)
|
||||
await assert.rejects(readServerSamplesInRange(52, previous.toISOString(), { maxRows: 1 }), /предел/)
|
||||
await assert.rejects(readServerSamplesInRange(52, previous.toISOString(), { maxRows: 0 }), /предел/)
|
||||
const clipped = await readServerSamplesInRange(52, new Date(previous.getTime() + 15_000).toISOString(), {
|
||||
untilIso: sourceAt.toISOString(), maxRows: 2, interfaceNames: ["fixture-traffic-zero"], interfaceCountersOnly: true, precedingMaxIntervalMs: 90_000,
|
||||
})
|
||||
assert.deepEqual(clipped.map(row => new Date(row.sampledAt).toISOString()), [previous.toISOString(), sourceAt.toISOString()])
|
||||
const access = await makeAccess([52, 54]), principal = await authenticateMcpToken(access.token)
|
||||
const result = await invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 }) as History
|
||||
assert.equal(result.rxNowMbps, 0); assert.equal(result.txNowMbps, 0); assert.equal(result.accountedRxGiB, 0); assert.equal(result.currentState, "current"); assert.equal(result.live, false)
|
||||
@@ -89,6 +93,9 @@ export async function verifyMcpTrafficHistory(actor: AuthUser) {
|
||||
finally { pool.query = originalQuery; await pool.query("UPDATE servers SET host='192.0.2.52' WHERE id=52") }
|
||||
await pool.query("INSERT INTO traffic_samples(server_id,interface_name,sampled_at,flags) SELECT 52,'limit-history-'||n,$1,0 FROM generate_series(1,20001) n", [sourceAt.toISOString()])
|
||||
await assert.rejects(invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 }), /20000/)
|
||||
const filtered = await invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52, interface: "fixture-traffic-zero", range: "24h" }) as History
|
||||
assert.equal(filtered.accountedRxGiB, 0)
|
||||
assert(filtered.rxSeriesMbps.some(value => value === 0))
|
||||
await pool.query("DELETE FROM traffic_samples WHERE server_id=52 AND interface_name LIKE 'limit-history-%'")
|
||||
let ownerRevokedDuringRead = false
|
||||
pool.query = ((...args: unknown[]) => {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } fro
|
||||
import { useSearchParams } from "next/navigation"
|
||||
import Link from "next/link"
|
||||
import { useTable, type ColumnDef } from "@tanstack/react-table"
|
||||
import { BotIcon, CopyIcon, PlusIcon, RefreshCwIcon, ShieldIcon, StopCircleIcon } from "lucide-react"
|
||||
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 { routeAiControllerBlockReason, routeAiProfileSchema, type RouteAiProfile } from "@mmapp/contracts/mcp-routing"
|
||||
@@ -25,12 +25,18 @@ import { Badge } from "@/components/reui/badge"
|
||||
import { Button, buttonVariants } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Checkbox } from "@/components/ui/checkbox"
|
||||
import { Item, ItemContent, ItemDescription, ItemMedia, ItemTitle } from "@/components/ui/item"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs"
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog"
|
||||
|
||||
const errorText = (error: unknown) => error instanceof Error ? error.message : "Не удалось выполнить запрос"
|
||||
const modeLabels = { observer: "Наблюдатель", approval: "С подтверждением", autonomous: "Автономный" }
|
||||
const operationSectionLabels: Record<string, string> = {
|
||||
backups: "Резервные копии", certificates: "Сертификаты", filters: "Фильтры маршрутизации", firewall: "Межсетевой экран",
|
||||
ipsec: "IPsec", logs: "Журналы", mcp: "Управление MCP", network: "Сеть и маршрутизация", "recursive-routes": "Рекурсивные маршруты",
|
||||
servers: "Серверы", settings: "Настройки", traffic: "Трафик", uptime: "Доступность", users: "Пользователи", wireguard: "WireGuard",
|
||||
}
|
||||
const tabs = { overview: "Обзор", connections: "Подключения", permissions: "Разрешения", devices: "Интерфейсы", requests: "Обращения", plans: "Планы и операции", routing: "Route AI", settings: "Настройки" }
|
||||
type Tab = keyof typeof tabs
|
||||
type RecordRow = Record<string, unknown>
|
||||
@@ -152,30 +158,126 @@ function DeviceEditor({ api, connectionId, serverId, resource }: { api: McpApi;
|
||||
function TokenDialog({ token, close, connectionName }: { token: string; close: () => void; connectionName?: string }) {
|
||||
return <Dialog open={!!token} onOpenChange={open => { if (!open) close() }}><DialogContent className="sm:max-w-xl"><DialogHeader><DialogTitle>Доступ создан</DialogTitle><DialogDescription>{connectionName && `Подключение «${connectionName}». `}Сохраните токен в настройках AI-клиента. Повторно посмотреть его нельзя.</DialogDescription></DialogHeader><Input aria-label="Одноразовый токен MCP" readOnly type="password" value={token} /><Button onClick={() => { void navigator.clipboard.writeText(token).then(() => toast.success("Токен скопирован"), () => toast.error("Не удалось скопировать токен")) }}><CopyIcon />Копировать</Button><Button variant="outline" onClick={close}>Закрыть и скрыть токен</Button></DialogContent></Dialog>
|
||||
}
|
||||
function OperationChoices({ operations, selected, onChange, selectAllLabel = "Выбрать все операции" }: {
|
||||
operations: McpOperationDescriptor[]
|
||||
selected: string[]
|
||||
onChange: (ids: string[]) => void
|
||||
selectAllLabel?: string
|
||||
}) {
|
||||
const selectedIds = new Set(selected)
|
||||
const bySection = new Map<string, McpOperationDescriptor[]>()
|
||||
for (const operation of operations) {
|
||||
const group = bySection.get(operation.section)
|
||||
if (group) group.push(operation)
|
||||
else bySection.set(operation.section, [operation])
|
||||
}
|
||||
const groups = [...bySection]
|
||||
.sort(([left], [right]) => (operationSectionLabels[left] ?? left).localeCompare(operationSectionLabels[right] ?? right, "ru"))
|
||||
const selectOperations = (operations: McpOperationDescriptor[], checked: boolean) => {
|
||||
const groupIds = new Set(operations.map(operation => operation.id))
|
||||
onChange(checked ? [...new Set([...selected, ...groupIds])] : selected.filter(id => !groupIds.has(id)))
|
||||
}
|
||||
return <>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<Button variant="outline" size="sm" disabled={!operations.length} onClick={() => onChange(operations.map(operation => operation.id))}>{selectAllLabel}</Button>
|
||||
<Button variant="ghost" size="sm" disabled={!selected.length} onClick={() => onChange([])}>Снять все</Button>
|
||||
<span className="text-sm text-muted-foreground" role="status">Выбрано {selected.length}</span>
|
||||
</div>
|
||||
<div className="max-h-80 space-y-2 overflow-y-auto">
|
||||
{groups.map(([section, items]) => {
|
||||
const title = operationSectionLabels[section] ?? section
|
||||
const count = items.filter(operation => selectedIds.has(operation.id)).length
|
||||
return <div key={section} className="grid grid-cols-[auto_minmax(0,1fr)] items-start gap-3 rounded-md border p-3">
|
||||
<Checkbox aria-label={`Все операции группы «${title}»`} className="mt-0.5" checked={count === items.length} indeterminate={count > 0 && count < items.length} onCheckedChange={checked => selectOperations(items, checked)} />
|
||||
<details>
|
||||
<summary className="cursor-pointer text-sm font-medium">{title} <span className="ms-1 font-normal text-muted-foreground">{count} / {items.length}</span></summary>
|
||||
<div className="mt-3 grid gap-3 sm:grid-cols-2">
|
||||
{items.map(operation => <Choice key={operation.id} label={operation.title} checked={selectedIds.has(operation.id)} onChange={checked => selectOperations([operation], checked)} />)}
|
||||
</div>
|
||||
</details>
|
||||
</div>
|
||||
})}
|
||||
{!operations.length && <p className="text-sm text-muted-foreground">Нет доступных операций.</p>}
|
||||
</div>
|
||||
</>
|
||||
}
|
||||
function ServerChoices({ catalog, selected, onChange, all, onAllChange, forGrant = false }: {
|
||||
catalog: { data: Awaited<ReturnType<McpApi["serverChoices"]>> | null; error: string; refresh: () => void }
|
||||
selected: number[]
|
||||
onChange: (ids: number[]) => void
|
||||
all: boolean
|
||||
onAllChange: (value: boolean) => void
|
||||
forGrant?: boolean
|
||||
}) {
|
||||
const servers = catalog.data?.servers ?? []
|
||||
const unknown = catalog.data ? selected.filter(id => !servers.some(server => server.id === id)) : []
|
||||
return <fieldset className="min-w-0 space-y-2">
|
||||
<legend className="mb-2 text-sm font-medium">{forGrant ? "Серверы временного допуска" : "Разрешённые серверы"}</legend>
|
||||
<Choice label={forGrant ? "Все серверы подключения" : "Все серверы"} checked={all} onChange={onAllChange} />
|
||||
{all ? <><p className="text-sm text-muted-foreground">{forGrant ? "Выбраны серверы, разрешённые основному подключению." : "Доступ получат все серверы, зарегистрированные на момент сохранения."}</p>{forGrant && <ErrorMessage message={catalog.error} />}</> : <>
|
||||
<div className="grid max-h-60 gap-2 overflow-y-auto p-1 sm:grid-cols-2" aria-label="Карточки серверов" role="group">
|
||||
{servers.map(server => {
|
||||
const active = selected.includes(server.id), title = server.name || server.host
|
||||
return <Item key={server.id} variant="outline" size="sm" className="cursor-pointer text-left hover:bg-muted aria-pressed:border-primary aria-pressed:bg-accent"
|
||||
render={<button type="button" aria-label={title} aria-pressed={active} />}
|
||||
onClick={() => onChange(active ? selected.filter(id => id !== server.id) : [...selected, server.id])}>
|
||||
<ItemMedia variant="icon"><ServerIcon /></ItemMedia>
|
||||
<ItemContent className="min-w-0"><ItemTitle className="block max-w-full truncate">{title}</ItemTitle><ItemDescription className="break-all">{server.host !== title ? `${server.host} · ` : ""}ID {server.id}{!server.enabled && " · отключён"}</ItemDescription></ItemContent>
|
||||
{active && <CheckIcon className="size-4 shrink-0" aria-hidden="true" />}
|
||||
</Item>
|
||||
})}
|
||||
{unknown.map(id => <Button key={id} variant="outline" onClick={() => onChange(selected.filter(value => value !== id))}>Убрать отсутствующий сервер №{id}</Button>)}
|
||||
</div>
|
||||
{catalog.error && <><ErrorMessage message={catalog.error} /><Button variant="outline" size="sm" onClick={catalog.refresh}>Обновить список серверов</Button></>}
|
||||
{!catalog.data && !catalog.error && <p role="status" className="text-sm text-muted-foreground">Загрузка серверов…</p>}
|
||||
{catalog.data && <p className="text-sm text-muted-foreground">{selected.length ? `Выбрано серверов: ${selected.length}` : "Серверы не выбраны. Доступ к ним закрыт."}</p>}
|
||||
</>}
|
||||
</fieldset>
|
||||
}
|
||||
function ConnectionForm({ api, operations, editing, close, created }: { api: McpApi; operations: McpOperationDescriptor[]; editing?: McpConnection; close: () => void; created: (token?: string) => void }) {
|
||||
const [step, setStep] = useState(1), [busy, setBusy] = useState(false), [error, setError] = useState("")
|
||||
const [name, setName] = useState(editing?.name ?? ""), [mode, setMode] = useState<McpConnection["policy"]["mode"]>(editing?.policy.mode ?? "observer")
|
||||
const [serverText, setServerText] = useState(editing?.policy.scope.serverIds.join(", ") ?? ""), [selected, setSelected] = useState<string[]>(editing?.policy.scope.operationIds ?? [])
|
||||
const [service, setService] = useState(editing?.policy.authority === "service")
|
||||
const [unattended, setUnattended] = useState(editing?.policy.allowUnattendedPlans === true)
|
||||
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 permissions = getClaims()?.permissions ?? []
|
||||
const eligible = operations.filter(operation => operation.implementation === "available" && operation.requiredPermissions.every(permission => hasPermission(permissions, permission)))
|
||||
const toggle = (id: string) => setSelected(ids => ids.includes(id) ? ids.filter(value => value !== id) : [...ids, id])
|
||||
const selectedIds = new Set(selected)
|
||||
const dangerous = eligible.filter(operation => operation.risk === "dangerous" && selectedIds.has(operation.id))
|
||||
const autonomousDangerIds = dangerous.filter(operation => dangerIds.includes(operation.id)).map(operation => operation.id)
|
||||
const save = async () => {
|
||||
setBusy(true); setError("")
|
||||
try {
|
||||
const input = mcpConnectionCreateSchema.parse({ name, mode, expiresAt: new Date(expires + "Z").toISOString(), permissions: [...new Set(eligible.filter(operation => selected.includes(operation.id)).flatMap(operation => operation.requiredPermissions))], scope: { serverIds: serverText.split(/[,\s]+/).filter(Boolean).map(Number), operationIds: selected, allowGlobal: global }, dangerousPolicy: danger, autonomousDangerousOperationIds: dangerIds.filter(id => selected.includes(id)), limits })
|
||||
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 })
|
||||
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) }
|
||||
}
|
||||
return <Dialog open onOpenChange={open => { if (!open && !busy) close() }}><DialogContent className="max-h-[90dvh] overflow-y-auto sm:max-w-3xl"><DialogHeader><DialogTitle>{editing ? "Изменить разрешения" : "Новое подключение"}</DialogTitle><DialogDescription>Шаг {step} из 4 · доступ ограничивается правами текущего владельца.</DialogDescription></DialogHeader><ErrorMessage message={error} />
|
||||
{step === 1 && <><Field label="Название"><Input value={name} onChange={event => setName(event.target.value)} placeholder="Агент диагностики" /></Field><Field label="Срок действия (UTC)"><Input type="datetime-local" value={expires} onChange={event => setExpires(event.target.value)} /></Field><NativeSelect label="Режим управления" value={mode} onChange={value => setMode(value as typeof mode)}>{Object.entries(modeLabels).map(([value, title]) => <option key={value} value={value}>{title}</option>)}</NativeSelect></>}
|
||||
{step === 2 && <><Field label="ID разрешённых серверов через запятую"><Input value={serverText} onChange={event => setServerText(event.target.value)} placeholder="1, 2" /></Field><Choice label="Разрешить выбранные глобальные операции" checked={global} onChange={setGlobal} /><div className="grid max-h-80 gap-2 overflow-y-auto sm:grid-cols-2">{eligible.map(operation => <Choice key={operation.id} label={operation.title} checked={selected.includes(operation.id)} onChange={() => toggle(operation.id)} />)}</div><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><NativeSelect label="Опасные действия" value={danger} onChange={value => setDanger(value as typeof danger)}><option value="approval">Всегда подтверждать</option><option value="explicit-allow">Автономно для явно выбранных действий</option></NativeSelect>{danger === "explicit-allow" && eligible.filter(operation => operation.risk === "dangerous" && selected.includes(operation.id)).map(operation => <Choice key={operation.id} label={operation.title} checked={dangerIds.includes(operation.id)} onChange={checked => setDangerIds(ids => checked ? [...ids, operation.id] : ids.filter(id => id !== operation.id))} />)}</>}
|
||||
{step === 4 && <div className="space-y-3 text-sm"><p><strong>{name || "Без названия"}</strong> · {modeLabels[mode]}</p><p>Серверы: {serverText || "не выбраны"}. Операций: {selected.length}. Глобальный доступ: {global ? "да" : "нет"}.</p><p>Защита объектов продолжает действовать независимо от автономных разрешений.</p><p>После сохранения проверьте конкретное действие в симуляторе на вкладке «Разрешения».</p></div>}
|
||||
{step === 1 && <><Field label="Название"><Input value={name} onChange={event => setName(event.target.value)} placeholder="Агент диагностики" /></Field>{!editing && <NativeSelect label="Тип подключения" value={service ? "service" : "personal"} onChange={value => setService(value === "service")}><option value="personal">Личное</option><option value="service">Сервисное — бессрочный автономный доступ</option></NativeSelect>}
|
||||
{service
|
||||
? <p className="text-sm text-muted-foreground">Подключение действует до отзыва. Способ подтверждения планов можно выбрать на шаге 3.</p>
|
||||
: <><Field label="Срок действия (UTC)"><Input type="datetime-local" value={expires} onChange={event => setExpires(event.target.value)} /></Field><NativeSelect label="Режим управления" value={mode} onChange={value => setMode(value as typeof mode)}>{Object.entries(modeLabels).map(([value, title]) => <option key={value} value={value}>{title}</option>)}</NativeSelect></>}</>}
|
||||
{step === 2 && <>
|
||||
<ServerChoices catalog={serverCatalog} selected={serverIds} onChange={setServerIds} all={selectAllServers} onAllChange={setSelectAllServers} />
|
||||
<Choice label="Разрешить выбранные глобальные операции" checked={global} onChange={setGlobal} />
|
||||
<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 ? <>
|
||||
<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>}
|
||||
<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>
|
||||
}
|
||||
@@ -196,9 +298,9 @@ function Connections({ api, operations }: { api: McpApi; operations: McpOperatio
|
||||
} },
|
||||
{ id: "mode", header: "Режим", cell: ({ row }) => modeLabels[(row.original.policy as McpConnection["policy"]).mode] },
|
||||
{ id: "status", header: "Состояние доступа", size: 260, cell: ({ row }) => <ConnectionAccessStatus connection={row.original as unknown as McpConnection} /> },
|
||||
{ id: "expiresAt", header: "Доступ до", size: 170, cell: ({ row }) => date((row.original.policy as McpConnection["policy"]).expiresAt) },
|
||||
{ id: "expiresAt", header: "Доступ до", size: 170, cell: ({ row }) => { const policy = row.original.policy as McpConnection["policy"]; return policy.authority === "service" ? "Бессрочно" : date(policy.expiresAt) } },
|
||||
{ accessorKey: "lastUsedAt", header: "Последний вызов", cell: ({ row }) => date(row.original.lastUsedAt) },
|
||||
{ id: "ownerValidation", header: "Права владельца до", size: 190, cell: ({ row }) => { const connection = row.original as unknown as McpConnection; return <div className="flex flex-col gap-1"><span className="text-xs">{date(connection.ownerValidUntil)}</span>{connection.ownerId === getClaims()?.sub && <Button size="sm" variant="outline" disabled={!connection.generationValid || connection.policy.status === "revoked"} onClick={() => { void act(() => api.revalidate(connection.id)) }}>Обновить мои права</Button>}</div> } },
|
||||
{ id: "ownerValidation", header: "Права владельца до", size: 190, cell: ({ row }) => { const connection = row.original as unknown as McpConnection; return connection.policy.authority === "service" ? "Сервисный доступ" : <div className="flex flex-col gap-1"><span className="text-xs">{date(connection.ownerValidUntil)}</span>{connection.ownerId === getClaims()?.sub && <Button size="sm" variant="outline" disabled={!connection.generationValid || connection.policy.status === "revoked"} onClick={() => { void act(() => api.revalidate(connection.id)) }}>Обновить мои права</Button>}</div> } },
|
||||
{ id: "actions", header: "Действия", size: 350, cell: ({ row }) => { const connection = row.original as unknown as McpConnection; return admin && <div className="flex flex-wrap gap-1"><Button size="sm" variant="outline" onClick={() => setEditing(connection)}>Разрешения</Button><Button size="sm" variant="outline" disabled={connection.policy.status === "revoked"} onClick={() => { void act(() => api.status(connection.id, connection.policy.status === "active" ? "paused" : "active")) }}>{connection.policy.status === "active" ? "Пауза" : "Включить"}</Button><Button size="sm" variant="outline" disabled={connection.policy.status === "revoked"} onClick={() => { if (window.confirm("Отозвать доступ подключения и все его токены?")) void act(() => api.status(connection.id, "revoked")) }}>Отозвать</Button><Button size="sm" variant="ghost" disabled={connection.policy.status === "revoked"} onClick={() => { if (window.confirm("Заменить токен? Старый токен перестанет работать.")) void act(async () => setToken((await api.rotate(connection.id)).token)) }}>Новый токен</Button></div> } },
|
||||
], [api, admin, act])
|
||||
return <><Panel title="Подключения" description="Токены, сроки и разрешения AI-клиентов" actions={admin && <Button onClick={() => setEditing("new")}><PlusIcon />Выдать доступ</Button>}><ErrorMessage message={query.error} /><Grid items={(query.data?.connections ?? []) as unknown as RecordRow[]} columns={columns} loading={!query.data && !query.error} /><p className="text-sm text-muted-foreground">HTTP endpoint: адрес backend + /mcp. Для stdio используйте MCP_URL и MCP_TOKEN с командой npm --prefix backend run mcp:stdio.</p></Panel>{editing && <ConnectionForm api={api} operations={operations} editing={editing === "new" ? undefined : editing} close={() => setEditing(null)} created={value => { setEditing(null); if (value) setToken(value); query.refresh() }} />}<TokenDialog token={token} close={() => setToken("")} /></>
|
||||
@@ -247,8 +349,11 @@ function ConnectionAccessTools({ api, connection, issue, grantBusy }: {
|
||||
grantBusy: boolean
|
||||
}) {
|
||||
const [result, setResult] = useState<Awaited<ReturnType<McpApi["check"]>> | null>(null)
|
||||
const [operations, setOperations] = useState<string[]>([]), [serverText, setServerText] = useState(""), [minutes, setMinutes] = useState(60), [global, setGlobal] = useState(false)
|
||||
const [serverIds, setServerIds] = useState<number[]>([]), [selectAllServers, setSelectAllServers] = useState(false)
|
||||
const [operations, setOperations] = useState<string[]>([]), [minutes, setMinutes] = useState(60), [global, setGlobal] = useState(false)
|
||||
const connectionId = connection.id
|
||||
const serverCatalog = useQuery(useCallback(signal => api.serverChoices(connectionId, signal), [api, connectionId]), connectionId)
|
||||
const grantServers = selectAllServers ? serverCatalog.data?.servers.map(server => server.id) ?? [] : serverIds
|
||||
const grants = useQuery(useCallback(signal => api.grants(connectionId, signal), [api, connectionId]))
|
||||
const checkRevision = useRef(0)
|
||||
useEffect(() => () => { checkRevision.current++ }, [])
|
||||
@@ -269,7 +374,7 @@ function ConnectionAccessTools({ api, connection, issue, grantBusy }: {
|
||||
try {
|
||||
void issue({
|
||||
id: crypto.randomUUID(), startsAt: now.toISOString(), expiresAt: expires.toISOString(),
|
||||
scope: { serverIds: serverText.split(",").map(value => value.trim()).filter(Boolean).map(Number), operationIds: operations, allowGlobal: global },
|
||||
scope: { serverIds: grantServers, operationIds: operations, allowGlobal: global },
|
||||
})
|
||||
} catch (error) {
|
||||
toast.error(errorText(error))
|
||||
@@ -278,7 +383,7 @@ function ConnectionAccessTools({ api, connection, issue, grantBusy }: {
|
||||
|
||||
return <>
|
||||
<ErrorMessage message={grants.error} />
|
||||
<p className="text-sm text-muted-foreground">Права владельца подтверждены до {date(connection.ownerValidUntil)}. Для продления войдите с актуальными правами портала; старые планы и допуски контроллера потребуется подготовить заново.</p>
|
||||
<p className="text-sm text-muted-foreground">{connection.policy.authority === "service" ? "Сервисный доступ действует до отзыва." : `Права владельца подтверждены до ${date(connection.ownerValidUntil)}. Для продления войдите с актуальными правами портала; старые планы и допуски контроллера потребуется подготовить заново.`}</p>
|
||||
<Button variant="outline" className="self-start" disabled={connection.ownerId !== getClaims()?.sub} onClick={() => { void api.revalidate(connectionId).then(() => { grants.refresh(); toast.success("Права владельца обновлены") }, error => toast.error(errorText(error))) }}>Подтвердить текущие права владельца</Button>
|
||||
<Button variant="outline" className="self-start" onClick={() => { void check() }}>Проверить готовность доступа</Button>
|
||||
{result !== null && <>
|
||||
@@ -287,13 +392,11 @@ function ConnectionAccessTools({ api, connection, issue, grantBusy }: {
|
||||
</p>}
|
||||
<pre className="overflow-auto rounded bg-muted p-3 text-xs">{pretty(result)}</pre>
|
||||
</>}
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
<Field label={`Серверы допуска (доступны: ${connection.policy.scope.serverIds.join(", ")})`}><Input value={serverText} onChange={event => setServerText(event.target.value)} placeholder="ID через запятую" /></Field>
|
||||
<Field label="Срок допуска, минут"><Input type="number" min={1} max={1440} value={minutes} onChange={event => setMinutes(Number(event.target.value))} /></Field>
|
||||
</div>
|
||||
<ServerChoices catalog={serverCatalog} selected={serverIds} onChange={setServerIds} all={selectAllServers} onAllChange={setSelectAllServers} forGrant />
|
||||
<Field label="Срок допуска, минут"><Input type="number" min={1} max={1440} value={minutes} onChange={event => setMinutes(Number(event.target.value))} /></Field>
|
||||
{connection.policy.scope.allowGlobal && <Choice label="Разрешить глобальные действия в допуске" checked={global} onChange={setGlobal} />}
|
||||
<div className="grid max-h-60 gap-2 overflow-y-auto sm:grid-cols-2">{connection.policy.scope.operationIds.map(id => <Choice key={id} label={id} checked={operations.includes(id)} onChange={checked => setOperations(values => checked ? [...values, id] : values.filter(value => value !== id))} />)}</div>
|
||||
<Button className="self-start" disabled={grantBusy || !operations.length || connection.policy.status !== "active"} onClick={create}>Выдать временный допуск</Button>
|
||||
<Button className="self-start" disabled={grantBusy || !operations.length || connection.policy.status !== "active" || (selectAllServers && !serverCatalog.data)} onClick={create}>Выдать временный допуск</Button>
|
||||
{grants.data?.grants.map(grant => <div key={grant.id} className="flex flex-wrap items-center justify-between gap-2 rounded border p-3 text-sm">
|
||||
<span>До {date(grant.grant.expiresAt)} · {grant.revoked_at ? "отозван" : "выдан"}</span>
|
||||
<Button size="sm" variant="outline" disabled={!!grant.revoked_at} onClick={() => { void api.revokeGrant(grant.id).then(grants.refresh, error => toast.error(errorText(error))) }}>Отозвать допуск</Button>
|
||||
|
||||
@@ -70,15 +70,17 @@ export function TrafficRxTxChart({
|
||||
rx,
|
||||
tx,
|
||||
range = "1h",
|
||||
rangeMinutes: actualMinutes,
|
||||
embedded = false,
|
||||
}: {
|
||||
rx: (number|null)[]
|
||||
tx: (number|null)[]
|
||||
range?: string
|
||||
rangeMinutes?: number
|
||||
/** Skip outer Frame when already inside OpsPanel / Frame. */
|
||||
embedded?: boolean
|
||||
}) {
|
||||
const rangeMinutes = TRAFFIC_RANGE_MINUTES[range] ?? 60
|
||||
const rangeMinutes = actualMinutes ?? TRAFFIC_RANGE_MINUTES[range] ?? 60
|
||||
const data = toChartData(rx, tx, rangeMinutes)
|
||||
const tickEvery = Math.max(1, Math.ceil(data.length / 6))
|
||||
|
||||
|
||||
@@ -430,6 +430,7 @@ export function FlowAnalyticsDetail({
|
||||
{range === "30d" && <p className="text-sm text-muted-foreground mb-3">Календарный месяц по UTC. Фильтры интерфейса, дублей, overlay и mesh к этому отчёту не применяются.</p>}
|
||||
{monthly && range === "30d" && <section aria-label="Месячный отчёт" className="flex flex-col gap-4">
|
||||
<p className="text-sm">Период: {monthly.month} (UTC) · Объём: {formatBytes(monthly.bytes)}</p>
|
||||
{monthly.totalsSource === "legacy-dimensions" && <p className="text-sm text-muted-foreground">Для части месяца объём оценён по сохранённым дневным сводкам.</p>}
|
||||
<div><h3 className="text-sm font-medium mb-2">Страны</h3><FlowBreakdownGrid rows={monthly.countries} country monthly /></div>
|
||||
<div><h3 className="text-sm font-medium mb-2">Сервисы</h3><FlowBreakdownGrid rows={monthly.services} monthly /></div>
|
||||
<div><h3 className="text-sm font-medium mb-2">ASN</h3><FlowBreakdownGrid rows={monthly.asns} monthly /></div>
|
||||
@@ -474,10 +475,19 @@ export function FlowAnalyticsDetail({
|
||||
) : null}
|
||||
|
||||
{analytics && <>
|
||||
{analytics.sourceWindow && <p role="status" className="mb-3 text-sm text-muted-foreground">Данные за последние {analytics.sourceWindow.effectiveMinutes} мин из запрошенных {analytics.sourceWindow.requestedMinutes} мин.</p>}
|
||||
<p className="mb-3 text-sm text-muted-foreground">
|
||||
RX/TX рассчитаны по сохранённой выборке потоков с известным направлением.
|
||||
{analytics.timeBasis === "collector-receipt" && " Время на графике соответствует приёму IPFIX коллектором."}
|
||||
</p>
|
||||
{analytics.admittedTotals && <p className="mb-3 text-sm text-muted-foreground">
|
||||
Коллектор принял {formatBytes(analytics.admittedTotals.bytes)} за завершённые минуты окна. Минут с данными — {analytics.admittedTotals.minutesWithData}. Фильтры overlay, mesh и дублей к этому счётчику не применяются.
|
||||
</p>}
|
||||
<TrafficRxTxChart
|
||||
rx={analytics?.rxSeries ?? card?.rxSeries ?? []}
|
||||
tx={analytics?.txSeries ?? card?.txSeries ?? []}
|
||||
range={range}
|
||||
rangeMinutes={analytics.sourceWindow?.effectiveMinutes}
|
||||
/>
|
||||
|
||||
<div className="mt-4 pt-4 border-t">
|
||||
@@ -486,7 +496,7 @@ export function FlowAnalyticsDetail({
|
||||
items={([
|
||||
{
|
||||
id: "bps-now",
|
||||
label: "Скорость сейчас",
|
||||
label: "Скорость IPFIX",
|
||||
value: fmtRate(rxNow),
|
||||
hint: liveHint,
|
||||
icon: <ArrowDownIcon className="size-4" />,
|
||||
@@ -494,9 +504,9 @@ export function FlowAnalyticsDetail({
|
||||
},
|
||||
{
|
||||
id: "bytes",
|
||||
label: "Payload",
|
||||
label: "Объём выборки",
|
||||
value: formatBytes(bytes),
|
||||
hint: "inner IPFIX",
|
||||
hint: "Сохранённые потоки",
|
||||
icon: <ArrowUpIcon className="size-4" />,
|
||||
iconClassName: "text-info",
|
||||
},
|
||||
|
||||
@@ -200,6 +200,7 @@ function NetflowSettingsContent({
|
||||
? new Date(settings.lastDatagramAt).toLocaleString("ru-RU")
|
||||
: settings ? "Не получен" : "—"}
|
||||
{settings?.lastExporterIp ? ` · ${settings.lastExporterIp}` : ""}
|
||||
{settings?.collectorCountersSource === "last-known" ? " · последние известные данные" : ""}
|
||||
{settings?.lastError ? ` · ${settings.lastError}` : ""}
|
||||
</p>
|
||||
|
||||
|
||||
@@ -65,6 +65,8 @@
|
||||
- **«Срок действия (UTC)»** — не больше года. Для первого шага достаточно нескольких дней.
|
||||
- **«Режим управления»** — «Наблюдатель». Он же выбран по умолчанию.
|
||||
- **Серверы и операции** — только те, что нужны агенту.
|
||||
- «Все серверы» включает серверы, зарегистрированные на момент сохранения. Новые серверы добавляются при повторном выборе и сохранении разрешений.
|
||||
- Для выборочного доступа отметьте карточки нужных серверов. Если «Все серверы» выключено и ни одна карточка не отмечена, доступ к серверам закрыт.
|
||||
- Операция, не выданная подключению, получает `OPERATION_DENIED`.
|
||||
- Сервер вне списка получает `SERVER_DENIED`.
|
||||
- Глобальные операции работают только с «Разрешить выбранные глобальные операции».
|
||||
@@ -117,6 +119,16 @@
|
||||
|
||||
Если в портале отозвать сессии пользователя, Manager отзывает и его подключения MCP вместе с токенами и временными допусками.
|
||||
|
||||
### Сервисное подключение
|
||||
|
||||
В «Подключения» → «Выдать доступ» администратор MCP может выбрать «Сервисное — бессрочный автономный доступ». Выберите нужные серверы и операции. Доступ действует до отзыва и переживает истечение обычной сессии портала. По умолчанию опасные действия требуют ручного подтверждения.
|
||||
|
||||
На шаге 3 можно включить «Выполнять планы без ручного подтверждения». Режим действует на все выданные ключу операции. Списки серверов, права, лимиты и правила защиты объектов продолжают действовать.
|
||||
|
||||
Если план содержит `announcement`, клиент сначала публикует отдельное сообщение в чате с ID плана, целями, изменениями и предупреждениями из `changes`. Затем вызывает `mm_change_apply` с `planId`, `idempotencyKey` и `announcedPlanDigest`, равным `announcement.digest`. Manager проверяет контрольную сумму сохранённого плана. Публикацию сообщения обеспечивает клиент. В аудите такой запуск отмечается как допуск по политике подключения.
|
||||
|
||||
Сервисный токен можно заменить, приостановить вместе с подключением или отозвать. Явный отзыв прав выдавшего доступ администратора в портале также отзывает его сервисные подключения. Чтобы сменить тип подключения, выдайте новый токен через новое подключение.
|
||||
|
||||
### Что агент может на этом шаге
|
||||
|
||||
- **Может** вызывать операции чтения: каталоги, состояние, журналы.
|
||||
|
||||
@@ -39,6 +39,12 @@ test("map Flow envelope retains zeros and refuses missing metadata and contradic
|
||||
assert.throws(()=>networkMapFlowSchema.parse({...empty,totalBytes:undefined}))
|
||||
assert.throws(()=>networkMapFlowSchema.parse({...empty,namedBytes:1}))
|
||||
assert.throws(()=>networkMapFlowSchema.parse({...empty,rangeMinutes:10}))
|
||||
const shorter={...empty,rangeMinutes:2,windowSec:120,sourceWindow:{requestedMinutes:5,effectiveMinutes:2}}
|
||||
assert.equal(networkMapFlowSchema.parse(shorter).rangeMinutes,2)
|
||||
assert.throws(()=>networkMapFlowSchema.parse({...shorter,sourceWindow:undefined}))
|
||||
assert.throws(()=>networkMapFlowSchema.parse({...shorter,sourceWindow:{requestedMinutes:15,effectiveMinutes:2}}))
|
||||
assert.throws(()=>networkMapFlowSchema.parse({...shorter,rangeMinutes:1,windowSec:60}))
|
||||
assert.throws(()=>networkMapFlowSchema.parse({...shorter,windowSec:300}))
|
||||
})
|
||||
test("map DNS matches exactly requested names and accepts explicit unresolved null",()=>{
|
||||
assert.deepEqual(validateNetworkMapDns({results:{"router.example":null}},["router.example"]),{"router.example":null})
|
||||
|
||||
@@ -27,7 +27,13 @@ export function validateNetworkMapSnapshot(servers:unknown,gre:unknown,probes:un
|
||||
|
||||
import {flowMapHopsDtoSchema} from "@mmapp/contracts/traffic-flow"
|
||||
import {isIpv4String} from "./gre-endpoint-resolve"
|
||||
export const networkMapFlowSchema=flowMapHopsDtoSchema.required().refine(data=>data.rangeMinutes===5&&data.namedBytes<=data.totalBytes&&data.unclassifiedBytes<=data.totalBytes)
|
||||
export const networkMapFlowSchema = flowMapHopsDtoSchema.required().partial({ sourceWindow: true }).refine(data =>
|
||||
(data.sourceWindow
|
||||
? data.sourceWindow.requestedMinutes === 5 && data.sourceWindow.effectiveMinutes === data.rangeMinutes
|
||||
: data.rangeMinutes === 5) &&
|
||||
data.windowSec === data.rangeMinutes * 60 &&
|
||||
data.namedBytes <= data.totalBytes && data.unclassifiedBytes <= data.totalBytes
|
||||
)
|
||||
export function validateNetworkMapDns(raw:unknown,hosts:string[]){
|
||||
const data=z.object({results:z.record(z.string(),z.string().refine(isIpv4String).nullable())}).parse(raw)
|
||||
const requested=new Set(hosts.map(host=>host.toLowerCase()))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import assert from "node:assert/strict"
|
||||
import { projectTrafficObservations } from "./traffic-observations.js"
|
||||
import { peakOfCombinedTraffic, projectTrafficObservations } from "./traffic-observations.js"
|
||||
const at=Date.parse("2026-10-12T12:00:00Z"),sampledAt=new Date(at).toISOString(),expiresAt=new Date(at+90_000).toISOString()
|
||||
const row={rxNow:0,txNow:0,rxPeak:0,txPeak:0,rxTotal:0,txTotal:0,rxSeries:[0,0],txSeries:[0,0]}
|
||||
const obs={rxNow:true,txNow:true,rxSeries:[false,true],txSeries:[false,true],sampledAt,currentState:"current",expiresAt}
|
||||
@@ -16,3 +16,8 @@ const noPeak=projectTrafficObservations({...row,volumeUnit:"GiB",observations:{.
|
||||
assert.equal(noPeak.rxPeak,null);assert.equal(noPeak.txPeak,null);assert.equal(noPeak.rxTotal,0)
|
||||
assert.equal(projectTrafficObservations({...row,observations:{...obs,rxPeak:true,txPeak:true}},at).rxPeak,0)
|
||||
console.log('aggregate peak confidence overrides bucket confidence; measured zero and volumes retained')
|
||||
assert.equal(peakOfCombinedTraffic([[10, 3, 20], [4, 15, null]]), 18)
|
||||
assert.equal(peakOfCombinedTraffic([[0, null], [0, 50]]), 0)
|
||||
assert.equal(peakOfCombinedTraffic([[null, 10], [4, null]]), null)
|
||||
assert.equal(peakOfCombinedTraffic([[2], [4, 5]]), null)
|
||||
assert.equal(peakOfCombinedTraffic([]), null)
|
||||
@@ -6,6 +6,20 @@ const meta = z.object({ rxNow: z.boolean(), txNow: z.boolean(), rxSeries: z.arra
|
||||
sampledAt: timestamp, expiresAt: timestamp.optional(), lastAttemptAt: timestamp.optional(), lastSucceededAt: timestamp.optional(), error: z.string().nullable().optional(),
|
||||
currentState: z.enum(["current", "unknown", "pending", "error", "stale", "disabled"]).optional() })
|
||||
export type TrafficObservationMeta = z.infer<typeof meta>
|
||||
|
||||
/** Sum only intervals with an observation from every series. */
|
||||
export function peakOfCombinedTraffic(series: readonly (readonly (number | null)[])[]): number | null {
|
||||
const length = series[0]?.length
|
||||
if (!length || series.some(values => values.length !== length)) return null
|
||||
let peak: number | null = null
|
||||
for (let index = 0; index < length; index++) {
|
||||
const slots = series.map(values => values[index])
|
||||
if (slots.some(value => value == null)) continue
|
||||
const sum = slots.reduce<number>((total, value) => total + (value ?? 0), 0)
|
||||
peak = peak === null ? sum : Math.max(peak, sum)
|
||||
}
|
||||
return peak
|
||||
}
|
||||
const metrics = z.object({ volumeUnit: z.literal("GiB").optional(), rxNow: rate, txNow: rate, rxPeak: rate, txPeak: rate, rxTotal: rate, txTotal: rate,
|
||||
rxSeries: z.array(rate), txSeries: z.array(rate), observations: meta.optional() }).refine(r =>
|
||||
(!r.observations || r.observations.rxSeries.length === r.rxSeries.length && r.observations.txSeries.length === r.txSeries.length &&
|
||||
|
||||
@@ -5,6 +5,8 @@ const serverId = z.number().int().positive().max(Number.MAX_SAFE_INTEGER)
|
||||
const instant = z.string().datetime({ offset: true })
|
||||
const uniqueStrings = z.array(identifier).max(1000).refine(values => new Set(values).size === values.length, "Повторяющиеся значения")
|
||||
const serverIds = z.array(serverId).max(1000).refine(values => new Set(values).size === values.length, "Повторяющиеся серверы")
|
||||
// Keep the existing finite timestamp contract for policy, credential and SQL proofs.
|
||||
export const MCP_SERVICE_EXPIRATION = "9999-12-31T23:59:59.000Z"
|
||||
|
||||
export const mcpModeSchema = z.enum(["observer", "approval", "autonomous"])
|
||||
export const mcpLimitsSchema = z.object({
|
||||
@@ -32,6 +34,8 @@ export const mcpConnectionPolicySchema = z.object({
|
||||
id: identifier,
|
||||
version: z.number().int().positive(),
|
||||
status: z.enum(["active", "paused", "revoked"]),
|
||||
authority: z.literal("service").optional(),
|
||||
allowUnattendedPlans: z.literal(true).optional(),
|
||||
expiresAt: instant,
|
||||
mode: mcpModeSchema,
|
||||
permissions: uniqueStrings,
|
||||
@@ -39,7 +43,10 @@ export const mcpConnectionPolicySchema = z.object({
|
||||
dangerousPolicy: z.enum(["approval", "explicit-allow"]).default("approval"),
|
||||
autonomousDangerousOperationIds: uniqueStrings.default([]),
|
||||
limits: mcpLimitsSchema.default(() => mcpLimitsSchema.parse({})),
|
||||
}).strict()
|
||||
}).strict().refine(policy => policy.authority === "service"
|
||||
? policy.expiresAt === MCP_SERVICE_EXPIRATION && policy.mode === "autonomous" && policy.dangerousPolicy === "approval" && !policy.autonomousDangerousOperationIds.length
|
||||
: !policy.allowUnattendedPlans,
|
||||
"Некорректная политика сервисного подключения")
|
||||
|
||||
export const mcpTargetSchema = z.discriminatedUnion("scope", [
|
||||
z.object({ scope: z.literal("servers"), serverIds: serverIds.refine(ids => ids.length > 0, "Выберите серверы") }).strict(),
|
||||
@@ -127,14 +134,20 @@ export type McpPolicyReasonCode = z.infer<typeof mcpPolicyReasonCodeSchema>
|
||||
|
||||
export const mcpConnectionCreateSchema = z.object({
|
||||
name: z.string().trim().min(1).max(100),
|
||||
expiresAt: instant,
|
||||
authority: z.literal("service").optional(),
|
||||
allowUnattendedPlans: z.literal(true).optional(),
|
||||
expiresAt: instant.nullable(),
|
||||
mode: mcpModeSchema.default("observer"),
|
||||
permissions: uniqueStrings,
|
||||
scope: mcpScopeSchema,
|
||||
selectAllServers: z.literal(true).optional(),
|
||||
dangerousPolicy: z.enum(["approval", "explicit-allow"]).default("approval"),
|
||||
autonomousDangerousOperationIds: uniqueStrings.default([]),
|
||||
limits: mcpLimitsSchema.default(() => mcpLimitsSchema.parse({})),
|
||||
}).strict()
|
||||
}).strict().refine(input => input.authority === "service"
|
||||
? input.expiresAt === null && input.mode === "autonomous" && input.dangerousPolicy === "approval" && !input.autonomousDangerousOperationIds.length
|
||||
: input.expiresAt !== null && !input.allowUnattendedPlans, "Опция доступна только бессрочному сервисному подключению. Для личного подключения укажите срок действия.")
|
||||
.refine(input => !input.selectAllServers || !input.scope.serverIds.length, "Выберите все серверы или укажите конкретные ID")
|
||||
export const mcpConnectionSchema = z.object({
|
||||
id: z.string().uuid(), name: z.string(), ownerId: z.string(),
|
||||
policy: mcpConnectionPolicySchema, createdAt: instant, updatedAt: instant,
|
||||
|
||||
@@ -59,6 +59,7 @@ export const flowIngestQualitySchema = z.object({
|
||||
|
||||
export const trafficFlowSettingsDtoSchema = z.object({
|
||||
dataQuality:flowIngestQualitySchema.nullable().optional(),
|
||||
collectorCountersSource: z.enum(["live", "last-known"]).optional(),
|
||||
enabled: z.boolean(),
|
||||
collectorIp: z.string().min(1),
|
||||
flowListenPort: z.number().int().positive(),
|
||||
@@ -163,8 +164,15 @@ export const flowTalkerDtoSchema = z.object({
|
||||
plane: z.string().optional(),
|
||||
})
|
||||
|
||||
const flowSourceWindowSchema = z.object({
|
||||
requestedMinutes: z.number().int().positive(),
|
||||
effectiveMinutes: z.number().int().positive(),
|
||||
}).strict().refine(value => value.effectiveMinutes < value.requestedMinutes)
|
||||
|
||||
export const flowStatsDtoSchema = z.object({
|
||||
sourceWindow: flowSourceWindowSchema.optional(),
|
||||
dataQuality:flowIngestQualitySchema.nullable().optional(),
|
||||
collectorCountersSource: z.enum(["live", "last-known"]).optional(),
|
||||
exportersOnline: z.number().int().nonnegative(),
|
||||
bytesPerMin: z.number().nonnegative(),
|
||||
uniqueSrc: z.number().int().nonnegative(),
|
||||
@@ -246,6 +254,15 @@ export const flowPathRowSchema = z.object({
|
||||
})
|
||||
|
||||
export const flowAnalyticsDtoSchema = z.object({
|
||||
sourceWindow: flowSourceWindowSchema.optional(),
|
||||
timeBasis: z.literal("collector-receipt").optional(),
|
||||
admittedTotals: z.object({
|
||||
bytes: z.number().int().nonnegative(),
|
||||
packets: z.number().int().nonnegative(),
|
||||
from: z.string().datetime(),
|
||||
to: z.string().datetime(),
|
||||
minutesWithData: z.number().int().positive(),
|
||||
}).optional(),
|
||||
bpsNow: z.number().nonnegative(),
|
||||
bytes: z.number().nonnegative(),
|
||||
packets: z.number().nonnegative(),
|
||||
@@ -284,6 +301,7 @@ export const flowAnalyticsDtoSchema = z.object({
|
||||
})
|
||||
|
||||
export const flowExportersDtoSchema = z.object({
|
||||
sourceWindow: flowSourceWindowSchema.optional(),
|
||||
exporters: z.array(flowEntityCardSchema),
|
||||
lastExporterIp: z.string().nullable().optional(),
|
||||
lastError: z.string().nullable().optional(),
|
||||
@@ -294,12 +312,14 @@ export const flowExportersDtoSchema = z.object({
|
||||
})
|
||||
|
||||
export const flowClientsDtoSchema = z.object({
|
||||
sourceWindow: flowSourceWindowSchema.optional(),
|
||||
clients: z.array(flowEntityCardSchema),
|
||||
})
|
||||
|
||||
export const flowMonthlyDtoSchema = z.object({
|
||||
month: z.string(),
|
||||
bytes: z.number().nonnegative(),
|
||||
totalsSource: z.enum(["admitted-daily", "legacy-dimensions"]).optional(),
|
||||
countries: z.array(flowBreakdownRowSchema),
|
||||
services: z.array(flowBreakdownRowSchema),
|
||||
asns: z.array(flowBreakdownRowSchema),
|
||||
@@ -390,6 +410,7 @@ export const flowMapCountryServiceGroupDtoSchema = z.object({
|
||||
})
|
||||
|
||||
export const flowMapHopsDtoSchema = z.object({
|
||||
sourceWindow: flowSourceWindowSchema.optional(),
|
||||
hops: z.array(flowMapHopDtoSchema),
|
||||
live: z.boolean(),
|
||||
rangeMinutes: z.number().int().positive(),
|
||||
|
||||
@@ -13,6 +13,8 @@ export function mcpApi(base: string) {
|
||||
const get = async <T>(path: string, schema: z.ZodType<T>, signal?: AbortSignal): Promise<T> => schema.parse(await requestJson(base, "/api/mcp" + path, { signal }))
|
||||
const send = async <T>(path: string, body: unknown, schema: z.ZodType<T>, method = "POST", signal?: AbortSignal): Promise<T> => schema.parse(await requestJson(base, "/api/mcp" + path, { method, body: JSON.stringify(body), signal }))
|
||||
return {
|
||||
serverChoices: (connectionId?: string, signal?: AbortSignal) => get(connectionId ? "/server-choices?" + new URLSearchParams({ connectionId }) : "/server-choices",
|
||||
deviceCatalogSchema.pick({ servers: true }), signal),
|
||||
downloadArtifact: (receipt: unknown, signal?: AbortSignal) => downloadHumanArtifact(base, receipt, signal),
|
||||
coverage: (signal?: AbortSignal) => get("/coverage", z.object({ version: z.literal(1), routes: z.array(z.object({ method: z.string(), path: z.string(), status: z.enum(["mediated", "excluded", "pending"]), operations: z.array(z.string()), reason: z.string() })) }), signal),
|
||||
uploadRestoreArchive: async (file: File, signal?: AbortSignal) => restoreArchiveSchema.parse(await requestJson(base, "/api/mcp/database/archives", { method: "POST", headers: { "Content-Type": "application/octet-stream" }, body: file, signal })),
|
||||
|
||||
@@ -24,8 +24,16 @@ async function setup(page: Page, permissions: string[], access: { expiresAt?: st
|
||||
if (method !== 'GET') mutations.push({ path, body: request.postDataJSON() })
|
||||
if (path === '/api/auth/config') return json({ required: true })
|
||||
if (path === '/api/mcp/catalog') return json({ stage: 'runtime', executionAvailable: true, operations: [read, forbidden] })
|
||||
if (path === '/api/mcp/server-choices') return json({ servers: [
|
||||
{ id: 1, name: 'CHR WAN', host: 'wan.example.test', enabled: true },
|
||||
{ id: 2, name: 'CHR LAN', host: 'lan.example.test', enabled: true },
|
||||
] })
|
||||
if (path === '/api/mcp/overview') return json({ connections: 1, calls: 3, errors: 1, approvals: 0, running: 0, enabled, observedAt: at })
|
||||
if (path === '/api/mcp/connections') { connectionReads++; const visible = { ...connection, ownerValidUntil }; return json(method === 'POST' ? { connection: visible, token: 'synthetic-one-time-token' } : { connections: [visible] }) }
|
||||
if (path === `/api/mcp/connections/${id}` && method === 'PUT') {
|
||||
const { name, selectAllServers, scope, ...policy } = request.postDataJSON()
|
||||
return json({ ...connection, name, policy: { ...connection.policy, ...policy, scope: selectAllServers ? { ...scope, serverIds: [1, 2] } : scope }, ownerValidUntil })
|
||||
}
|
||||
if (path === `/api/mcp/connections/${id}/status`) { connection.policy.status = request.postDataJSON().status; return json({ ok: true }) }
|
||||
if (path === '/api/mcp/stop-automation') { enabled = false; return json({ ok: true }) }
|
||||
if (path === '/api/mcp/requests') return json({ items: [{ id: 'synthetic-call', tool: 'mm_logs_query', created_at: at, connection_id: id, status: connection.policy.status === 'active' ? 'completed' : 'denied', error_code: connection.policy.status === 'active' ? null : `connection_${connection.policy.status}`, detail: 'synthetic fixture outcome' }], total: 1, nextCursor: null })
|
||||
@@ -292,13 +300,33 @@ test('synthetic: OAuth approval becomes unavailable when the selected connection
|
||||
|
||||
test('synthetic: issue only selected server/read permission and hide one-time token', async ({ page }) => {
|
||||
const state = await setup(page, ['mm:mcp:admin', 'mm:servers:read'])
|
||||
const logStatus = { ...read, id: 'logs.status', toolName: 'mm_logs_status', title: 'Статус журналов' }
|
||||
const capabilities = { ...read, id: 'capabilities.read', toolName: 'mm_capabilities_get', title: 'Доступные возможности', section: 'mcp', requiredPermissions: [] }
|
||||
await page.route('**/api/mcp/catalog', route => route.fulfill({ json: { stage: 'runtime', executionAvailable: true, operations: [read, logStatus, capabilities, forbidden] } }))
|
||||
await page.goto('/mcp?tab=connections')
|
||||
await page.getByRole('button', { name: 'Выдать доступ', exact: true }).click()
|
||||
const dialog = page.getByRole('dialog')
|
||||
await dialog.getByLabel('Название', { exact: true }).fill('Ограниченная диагностика')
|
||||
await dialog.getByRole('button', { name: 'Далее', exact: true }).click()
|
||||
await expect(dialog.getByRole('checkbox', { name: forbidden.title, exact: true })).toHaveCount(0)
|
||||
await dialog.getByLabel('ID разрешённых серверов через запятую').fill('1')
|
||||
const wan = dialog.getByRole('button', { name: 'CHR WAN', exact: true })
|
||||
await wan.click()
|
||||
await expect(wan).toHaveAttribute('aria-pressed', 'true')
|
||||
await expect(dialog.getByRole('button', { name: 'CHR LAN', exact: true })).toHaveAttribute('aria-pressed', 'false')
|
||||
await dialog.getByRole('button', { name: 'Выбрать все операции' }).click()
|
||||
await expect(dialog.getByRole('status')).toHaveText('Выбрано 3')
|
||||
await dialog.getByRole('button', { name: 'Снять все' }).click()
|
||||
await expect(dialog.getByRole('status')).toHaveText('Выбрано 0')
|
||||
const logsGroup = dialog.getByRole('checkbox', { name: 'Все операции группы «Журналы»' })
|
||||
await logsGroup.check()
|
||||
await expect(dialog.getByRole('status')).toHaveText('Выбрано 2')
|
||||
await dialog.locator('summary').filter({ hasText: 'Журналы' }).click()
|
||||
await expect(dialog.getByRole('checkbox', { name: read.title, exact: true })).toBeChecked()
|
||||
await dialog.getByRole('checkbox', { name: logStatus.title, exact: true }).uncheck()
|
||||
await expect(logsGroup).toHaveAttribute('aria-checked', 'mixed')
|
||||
await logsGroup.check()
|
||||
await logsGroup.uncheck()
|
||||
await expect(dialog.getByRole('status')).toHaveText('Выбрано 0')
|
||||
await dialog.getByRole('checkbox', { name: read.title, exact: true }).check()
|
||||
await dialog.getByRole('button', { name: 'Далее', exact: true }).click()
|
||||
await dialog.getByRole('button', { name: 'Далее', exact: true }).click()
|
||||
@@ -315,6 +343,79 @@ test('synthetic: issue only selected server/read permission and hide one-time to
|
||||
await expect(token).toHaveCount(0)
|
||||
})
|
||||
|
||||
test('synthetic: edit autonomous dangerous operations by group or all at once', async ({ page }) => {
|
||||
const state = await setup(page, ['mm:mcp:admin', 'mm:servers:read'])
|
||||
const danger = { ...read, effect: 'write', risk: 'dangerous' }
|
||||
const logs = { ...danger, id: 'test.logs.change', toolName: 'mm_test_logs_change', title: 'Изменить журналы' }
|
||||
const network = { ...danger, id: 'test.network.change', toolName: 'mm_test_network_change', title: 'Изменить сеть', section: 'network' }
|
||||
const unchecked = { ...network, id: 'test.network.other', toolName: 'mm_test_network_other', title: 'Другая операция сети' }
|
||||
await page.route('**/api/mcp/catalog', route => route.fulfill({ json: { stage: 'runtime', executionAvailable: true, operations: [read, logs, network, unchecked] } }))
|
||||
await page.goto('/mcp?tab=connections')
|
||||
await page.getByRole('row').filter({ hasText: 'Синтетический AI клиент' }).getByRole('button', { name: 'Разрешения', exact: true }).click()
|
||||
const dialog = page.getByRole('dialog', { name: 'Изменить разрешения' })
|
||||
const next = dialog.getByRole('button', { name: 'Далее', exact: true })
|
||||
await dialog.getByLabel('Режим управления', { exact: true }).selectOption('autonomous')
|
||||
await next.click()
|
||||
const allServers = dialog.getByRole('checkbox', { name: 'Все серверы', exact: true })
|
||||
const wan = dialog.getByRole('button', { name: 'CHR WAN', exact: true })
|
||||
await expect(allServers).not.toBeChecked()
|
||||
await expect(wan).toHaveAttribute('aria-pressed', 'true')
|
||||
await allServers.check()
|
||||
await expect(wan).toHaveCount(0)
|
||||
await allServers.uncheck()
|
||||
await expect(wan).toHaveAttribute('aria-pressed', 'true')
|
||||
await allServers.check()
|
||||
await dialog.getByRole('button', { name: 'Выбрать все операции', exact: true }).click()
|
||||
await dialog.locator('summary').filter({ hasText: 'Сеть и маршрутизация' }).click()
|
||||
await dialog.getByRole('checkbox', { name: unchecked.title, exact: true }).uncheck()
|
||||
await next.click()
|
||||
await dialog.getByLabel('Опасные действия', { exact: true }).selectOption('explicit-allow')
|
||||
const selectAll = dialog.getByRole('button', { name: 'Выбрать все опасные операции' })
|
||||
await selectAll.click()
|
||||
await expect(dialog.getByRole('status')).toHaveText('Выбрано 2')
|
||||
await dialog.getByRole('button', { name: 'Снять все' }).click()
|
||||
await dialog.getByRole('checkbox', { name: 'Все операции группы «Журналы»' }).check()
|
||||
await expect(dialog.getByRole('status')).toHaveText('Выбрано 1')
|
||||
await selectAll.click()
|
||||
await next.click()
|
||||
await expect(dialog.getByText(/Серверы: все на момент сохранения/)).toBeVisible()
|
||||
await expect(dialog.getByText('Опасных операций выбрано для автономного выполнения: 2.')).toBeVisible()
|
||||
await dialog.getByRole('button', { name: 'Сохранить доступ' }).click()
|
||||
await expect(dialog).toBeHidden()
|
||||
expect(state.mutations).toEqual([expect.objectContaining({ path: `/api/mcp/connections/${id}`, body: expect.objectContaining({
|
||||
selectAllServers: true,
|
||||
scope: { serverIds: [], operationIds: [read.id, logs.id, network.id], allowGlobal: false },
|
||||
autonomousDangerousOperationIds: [logs.id, network.id],
|
||||
}) })])
|
||||
})
|
||||
|
||||
for (const unattended of [false, true]) test(`synthetic: issue service access with unattended plans ${unattended ? 'enabled' : 'disabled'}`, async ({ page }) => {
|
||||
const state = await setup(page, ['mm:mcp:admin', 'mm:servers:read'])
|
||||
await page.goto('/mcp?tab=connections')
|
||||
await page.getByRole('button', { name: 'Выдать доступ', exact: true }).click()
|
||||
const dialog = page.getByRole('dialog')
|
||||
const next = dialog.getByRole('button', { name: 'Далее', exact: true })
|
||||
await dialog.getByLabel('Название', { exact: true }).fill('Служебный мониторинг')
|
||||
await dialog.getByLabel('Тип подключения', { exact: true }).selectOption('service')
|
||||
await expect(dialog.getByLabel('Срок действия (UTC)', { exact: true })).toHaveCount(0)
|
||||
await next.click()
|
||||
await dialog.getByRole('button', { name: 'CHR WAN', exact: true }).click()
|
||||
await dialog.getByRole('checkbox', { name: 'Все операции группы «Журналы»' }).check()
|
||||
await next.click()
|
||||
const unattendedChoice = dialog.getByRole('checkbox', { name: 'Выполнять планы без ручного подтверждения', exact: true })
|
||||
await expect(unattendedChoice).not.toBeChecked()
|
||||
if (unattended) await unattendedChoice.check()
|
||||
await next.click()
|
||||
await expect(dialog.getByText('Сервисное, бессрочное', { exact: false })).toBeVisible()
|
||||
await dialog.getByRole('button', { name: 'Сохранить доступ', exact: true }).click()
|
||||
await expect(page.getByLabel('Одноразовый токен MCP')).toHaveValue('synthetic-one-time-token')
|
||||
expect(state.mutations).toEqual([expect.objectContaining({ path: '/api/mcp/connections', body: expect.objectContaining({
|
||||
authority: 'service', expiresAt: null, mode: 'autonomous', dangerousPolicy: 'approval', autonomousDangerousOperationIds: [],
|
||||
scope: { serverIds: [1], operationIds: ['logs.query'], allowGlobal: false },
|
||||
}) })])
|
||||
expect(state.mutations[0].body.allowUnattendedPlans).toBe(unattended ? true : undefined)
|
||||
})
|
||||
|
||||
test('synthetic: pause, resume, cancelled revoke, revoke, monitor denied outcome, stop automation', async ({ page }) => {
|
||||
const state = await setup(page, ['mm:mcp:admin', 'mm:mcp:pause', 'mm:servers:read'])
|
||||
await page.goto('/mcp?tab=connections')
|
||||
|
||||
@@ -17,6 +17,7 @@ async function setup(page: Page, fail = false) {
|
||||
const json = (body: unknown) => route.fulfill({ headers, json: body })
|
||||
if (path === "/api/auth/config") return json({ required: false })
|
||||
if (path === "/api/mcp/catalog") return json({ stage: "runtime", executionAvailable: true, operations: [operation] })
|
||||
if (path === "/api/mcp/server-choices") return json({ servers: [{ id: 1, name: "CHR lab", host: "192.0.2.1", enabled: true }] })
|
||||
if (path === "/api/mcp/coverage") return json({ version: 1, routes: [{ method: "POST", path: "/api/geoip/update", status: "pending", operations: [], reason: "Нужен отдельный сервисный адаптер" }] })
|
||||
if (path === "/api/mcp/overview") return fail ? route.fulfill({ headers, status: 503, json: { error: "Аудит временно недоступен" } }) : json({ connections: 1, calls: 7, errors: 1, approvals: 1, running: 0, enabled: true, observedAt: at })
|
||||
if (path === "/api/mcp/connections") return json(method === "POST" ? { connection, token: "test-one-time-secret" } : { connections: [connection] })
|
||||
@@ -414,7 +415,8 @@ test("MCP: доступ, проверка подключения, выдача
|
||||
await page.getByRole("button", { name: "Выдать доступ", exact: true }).click()
|
||||
await page.getByLabel("Название", { exact: true }).fill("Новый AI клиент")
|
||||
await page.getByRole("button", { name: "Далее", exact: true }).click()
|
||||
await page.getByLabel("ID разрешённых серверов через запятую").fill("1")
|
||||
await page.getByRole("button", { name: "CHR lab", exact: true }).click()
|
||||
await page.locator("summary").filter({ hasText: "Журналы" }).click()
|
||||
await page.getByRole("checkbox", { name: "Поиск логов", exact: true }).check()
|
||||
await page.getByRole("button", { name: "Далее", exact: true }).click()
|
||||
await page.getByRole("button", { name: "Далее", exact: true }).click()
|
||||
@@ -477,13 +479,19 @@ test("MCP: временные допуски и результаты прове
|
||||
await expect(revoke).toBeVisible()
|
||||
await Promise.all([page.waitForRequest(`**/api/mcp/connections/${id}/check`), check.click()])
|
||||
await page.getByRole("checkbox", { name: "logs.query", exact: true }).check()
|
||||
await Promise.all([
|
||||
const serverCard = page.getByRole("button", { name: "CHR lab", exact: true })
|
||||
await serverCard.click()
|
||||
await expect(serverCard).toHaveAttribute("aria-pressed", "true")
|
||||
await page.getByRole("checkbox", { name: "Все серверы подключения", exact: true }).check()
|
||||
const [issuedRequest] = await Promise.all([
|
||||
page.waitForRequest(request => request.method() === "POST" && request.url().endsWith(`/connections/${id}/grants`)),
|
||||
create.click(),
|
||||
])
|
||||
expect(issuedRequest.postDataJSON().scope.serverIds).toEqual([1])
|
||||
|
||||
await Promise.all([page.waitForRequest(`**/api/mcp/connections/${otherId}/grants`), selection.selectOption(otherId)])
|
||||
await expect(revoke).toHaveCount(0)
|
||||
await expect(serverCard).toHaveAttribute("aria-pressed", "false")
|
||||
await page.getByRole("checkbox", { name: "logs.query", exact: true }).check()
|
||||
await expect(create).toBeDisabled()
|
||||
releaseOtherGrants()
|
||||
|
||||
@@ -5,12 +5,13 @@ test.beforeEach(async({page})=>{await page.route("**/health",r=>r.fulfill({json:
|
||||
const server={id:1,name:"Router",host:"192.0.2.1",type:"home-router",country:"RU",enabled:true,status:"online"}
|
||||
for(const malformed of [false,true])test("Карта: NetFlow отказ и повтор "+malformed,async({page})=>{
|
||||
let failing=true
|
||||
await page.route("**/api/**",r=>{const p=new URL(r.request().url()).pathname;return r.fulfill(p===flowPath?(failing?(malformed?{json:{}}:{status:503,json:{error:"NetFlow недоступен"}}):{json:empty}):{json:p==="/api/servers"?[server]:p==="/api/filters/gre-tunnels"?{tunnels:[],failures:[]}:p==="/api/uptime/speed-probes"?{probes:[]}:p==="/api/auth/config"?{required:false}:{}})})
|
||||
const recovered=malformed?{...empty,rangeMinutes:2,windowSec:120,sourceWindow:{requestedMinutes:5,effectiveMinutes:2}}:empty
|
||||
await page.route("**/api/**",r=>{const p=new URL(r.request().url()).pathname;return r.fulfill(p===flowPath?(failing?(malformed?{json:{}}:{status:503,json:{error:"NetFlow недоступен"}}):{json:recovered}):{json:p==="/api/servers"?[server]:p==="/api/filters/gre-tunnels"?{tunnels:[],failures:[]}:p==="/api/uptime/speed-probes"?{probes:[]}:p==="/api/auth/config"?{required:false}:{}})})
|
||||
await page.goto("/network-map")
|
||||
await expect(page.getByRole("alert").filter({hasText:"Первоначальная загрузка NetFlow"})).toBeVisible()
|
||||
await expect(page.getByText("Данные NetFlow неизвестны",{exact:true})).toBeVisible()
|
||||
failing=false;await page.getByRole("button",{name:"Обновить",exact:true}).click()
|
||||
await expect(page.getByText(/NetFlow · 5 мин · ответ/)).toContainText("приёмник выключен")
|
||||
await expect(page.getByText(malformed?/NetFlow · 2 мин из 5 запрошенных · ответ/:/NetFlow · 5 мин · ответ/)).toContainText("приёмник выключен")
|
||||
await expect(page.getByRole("alert").filter({hasText:"NetFlow:"})).toHaveCount(0)
|
||||
})
|
||||
test("Карта: NetFlow сохраняет сервис при фоновом сбое",async({page})=>{
|
||||
|
||||
Loaded 100 of 101 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user